Last active
December 19, 2015 01:59
-
-
Save jlewin/5880191 to your computer and use it in GitHub Desktop.
Very rough prototype for remote volume management on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.hifov.remotevolumecontrol; | |
| import java.io.BufferedReader; | |
| import java.io.IOException; | |
| import java.io.InputStreamReader; | |
| import java.net.InetAddress; | |
| import java.net.NetworkInterface; | |
| import java.net.ServerSocket; | |
| import java.net.Socket; | |
| import java.net.SocketException; | |
| import java.text.DateFormat; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Date; | |
| import java.util.Enumeration; | |
| import com.hifov.remotevolumecontrol.R; | |
| import android.media.AudioManager; | |
| import android.net.wifi.WifiInfo; | |
| import android.net.wifi.WifiManager; | |
| import android.os.Bundle; | |
| import android.os.Handler; | |
| import android.os.Message; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.text.format.Time; | |
| import android.util.Log; | |
| import android.view.Menu; | |
| import android.view.View; | |
| import android.view.View.OnClickListener; | |
| import android.widget.Button; | |
| import android.widget.TextView; | |
| public class ServerActivity extends Activity { | |
| private static final int VOLUME_CONTROL = 1; | |
| private static int maxVolume; | |
| private TextView serverStatus, runningStatus; | |
| private ServerSocket serverSocket; | |
| private Button toggleServer; | |
| // DEFAULT IP | |
| public static String SERVERIP = "10.0.2.150"; | |
| // DESIGNATE A PORT | |
| public static final int SERVERPORT = 8080; | |
| private AudioManager audioManager; | |
| private WifiManager wifiManager; | |
| private Handler handler = new Handler(){ | |
| public void handleMessage(Message msg) { | |
| int newLevel = msg.arg1; | |
| if(newLevel >= 0 && newLevel <= maxVolume) | |
| { | |
| audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newLevel, AudioManager.FLAG_SHOW_UI); | |
| serverStatus.setText("Volume set to " + newLevel); | |
| } | |
| } | |
| }; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_server); | |
| toggleServer = (Button) findViewById(R.id.toggle_service); | |
| serverStatus = (TextView) findViewById(R.id.server_status); | |
| runningStatus = (TextView) findViewById(R.id.running_status); | |
| Context context = getBaseContext(); | |
| // Grab audio and wifi system services | |
| audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); | |
| wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); | |
| maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); | |
| runningStatus.setText("Max Volume Level: " + maxVolume); | |
| SERVERIP = getWifiIPAddress(); | |
| serverStatus.setText(SERVERIP); | |
| toggleServer.setOnClickListener(onClickListener); | |
| } | |
| private OnClickListener onClickListener = new OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| switch(v.getId()){ | |
| case R.id.toggle_service: | |
| Thread fst = new Thread(new ServerThread()); | |
| fst.start(); | |
| break; | |
| } | |
| } | |
| }; | |
| @Override | |
| public boolean onCreateOptionsMenu(Menu menu) { | |
| // Inflate the menu; this adds items to the action bar if it is present. | |
| getMenuInflater().inflate(R.menu.server, menu); | |
| return true; | |
| } | |
| @Override | |
| protected void onStart(){ | |
| // TODO Auto-generated method stub | |
| super.onStart(); | |
| Log.d("starting", "On Start ....."); | |
| } | |
| @Override | |
| protected void onStop() { | |
| super.onStop(); | |
| try { | |
| // MAKE SURE YOU CLOSE THE SOCKET UPON EXITING | |
| serverSocket.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| private String getWifiIPAddress() { | |
| WifiInfo wifiInfo = wifiManager.getConnectionInfo(); | |
| int ip = wifiInfo.getIpAddress(); | |
| String ipString = String.format( | |
| "%d.%d.%d.%d", | |
| (ip & 0xff), | |
| (ip >> 8 & 0xff), | |
| (ip >> 16 & 0xff), | |
| (ip >> 24 & 0xff)); | |
| return ipString; | |
| } | |
| public class ServerThread implements Runnable { | |
| public void run() { | |
| try { | |
| if (SERVERIP != null) { | |
| handler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| runningStatus.setText("Listening on IP: " + SERVERIP); | |
| } | |
| }); | |
| serverSocket = new ServerSocket(SERVERPORT); | |
| while (true) { | |
| // LISTEN FOR INCOMING CLIENTS | |
| Socket client = serverSocket.accept(); | |
| handler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| serverStatus.setText("Connected."); | |
| } | |
| }); | |
| try { | |
| BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); | |
| String line = null; | |
| while ((line = in.readLine()) != null) { | |
| Log.d("ServerActivity", line); | |
| String[] segments = line.split(":"); | |
| if (segments[0].equals("level")) | |
| { | |
| int level = Integer.decode(segments[1]); | |
| Message test = handler.obtainMessage(VOLUME_CONTROL, level, 0); | |
| handler.sendMessage(test); | |
| } | |
| } | |
| break; | |
| } catch (Exception e) { | |
| handler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones."); | |
| } | |
| }); | |
| e.printStackTrace(); | |
| } | |
| } | |
| } else { | |
| handler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| serverStatus.setText("Couldn't detect internet connection."); | |
| } | |
| }); | |
| } | |
| } catch (Exception e) { | |
| handler.post(new Runnable() { | |
| @Override | |
| public void run() { | |
| runningStatus.setText(""); | |
| serverStatus.setText("Error"); | |
| } | |
| }); | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment