Ability to change brightness on device

This commit is contained in:
Victor 2013-07-11 19:34:28 +03:00
parent d48c49ed32
commit 22d0b2b9ae
4 changed files with 53 additions and 10 deletions

View File

@ -7,12 +7,18 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.text.format.Formatter; import android.text.format.Formatter;
import android.view.WindowManager;
import android.widget.TextView; import android.widget.TextView;
import com.annimon.socketfiletransfer.operations.Operation;
import com.annimon.socketfiletransfer.util.Console; import com.annimon.socketfiletransfer.util.Console;
public class ServerActivity extends Activity { public class ServerActivity extends Activity {
public static final int
UPDATE_CONSOLE = 1,
CHANGE_BRIGHTNESS = 2;
private TextView messagesHistory; private TextView messagesHistory;
@Override @Override
@ -23,14 +29,8 @@ public class ServerActivity extends Activity {
messagesHistory = (TextView) findViewById(R.id.messages_history); messagesHistory = (TextView) findViewById(R.id.messages_history);
messagesHistory.setText(Console.getAllText()); messagesHistory.setText(Console.getAllText());
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
messagesHistory.setText(Console.getAllText());
}
};
Console.setHandler(handler); Console.setHandler(handler);
Operation.setHandler(handler);
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE); WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
@ -41,5 +41,21 @@ public class ServerActivity extends Activity {
new SocketTransferTask(OperationListener.MODE_SERVER).execute(); new SocketTransferTask(OperationListener.MODE_SERVER).execute();
} }
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == UPDATE_CONSOLE) {
messagesHistory.setText(Console.getAllText());
} else if (msg.what == CHANGE_BRIGHTNESS) {
int brightness = msg.arg1;
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 100.0f;
getWindow().setAttributes(lp);
}
}
};
} }

View File

@ -1,6 +1,10 @@
package com.annimon.socketfiletransfer.operations; package com.annimon.socketfiletransfer.operations;
import android.os.Message;
import com.annimon.socketfiletransfer.OperationListener; import com.annimon.socketfiletransfer.OperationListener;
import com.annimon.socketfiletransfer.ServerActivity;
/** /**
* @author aNNiMON * @author aNNiMON
*/ */
@ -8,7 +12,21 @@ public class BrightnessOperation extends Operation {
@Override @Override
public void startServerSide() throws Exception { public void startServerSide() throws Exception {
String value = dis.readUTF();
int brightness;
try {
brightness = Integer.parseInt(value);
} catch (NumberFormatException ex) {
brightness = 30;
}
if (handler != null) {
Message msg = new Message();
msg.what = ServerActivity.CHANGE_BRIGHTNESS;
msg.arg1 = brightness;
handler.sendMessage(msg);
}
} }
@Override @Override

View File

@ -1,5 +1,7 @@
package com.annimon.socketfiletransfer.operations; package com.annimon.socketfiletransfer.operations;
import android.os.Handler;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@ -9,9 +11,15 @@ import java.io.DataOutputStream;
*/ */
public abstract class Operation { public abstract class Operation {
protected static Handler handler;
protected DataInputStream dis; protected DataInputStream dis;
protected DataOutputStream dos; protected DataOutputStream dos;
public static void setHandler(Handler handler) {
Operation.handler = handler;
}
public void setDataInputStream(DataInputStream dis) { public void setDataInputStream(DataInputStream dis) {
this.dis = dis; this.dis = dis;
} }

View File

@ -2,6 +2,8 @@ package com.annimon.socketfiletransfer.util;
import android.os.Handler; import android.os.Handler;
import com.annimon.socketfiletransfer.ServerActivity;
/** /**
* @author aNNiMON * @author aNNiMON
*/ */
@ -12,12 +14,11 @@ public class Console {
public static void print(String message) { public static void print(String message) {
allText.append(message); allText.append(message);
if (handler != null) handler.sendEmptyMessage(1); if (handler != null) handler.sendEmptyMessage(ServerActivity.UPDATE_CONSOLE);
} }
public static void println(String message) { public static void println(String message) {
print(message); print(message + "\r\n");
print("\r\n");
} }
public static String getAllText() { public static String getAllText() {