-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
168 changes: 168 additions & 0 deletions
168
app/src/main/java/com/github/ma1co/openmemories/tweak/DeveloperActivity.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package com.github.ma1co.openmemories.tweak; | ||
|
||
import android.content.BroadcastReceiver; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.IntentFilter; | ||
import android.net.ConnectivityManager; | ||
import android.net.NetworkInfo; | ||
import android.net.wifi.WifiInfo; | ||
import android.net.wifi.WifiManager; | ||
import android.os.Bundle; | ||
import android.text.TextUtils; | ||
import android.text.format.Formatter; | ||
import android.view.View; | ||
|
||
public class DeveloperActivity extends BaseActivity implements SwitchView.CheckedListener { | ||
public static final String[] telnetStartCommand = new String[] { "busybox", "telnetd", "-l", "sh" }; | ||
|
||
private ConnectivityManager connectivityManager; | ||
private WifiManager wifiManager; | ||
private BroadcastReceiver receiver; | ||
private SwitchView telnetSwitch; | ||
private SwitchView wifiSwitch; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_developer); | ||
|
||
telnetSwitch = (SwitchView) findViewById(R.id.telnet_switch); | ||
telnetSwitch.setListener(this); | ||
wifiSwitch = (SwitchView) findViewById(R.id.wifi_switch); | ||
wifiSwitch.setListener(this); | ||
|
||
connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); | ||
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); | ||
|
||
receiver = new BroadcastReceiver() { | ||
@Override | ||
public void onReceive(Context context, Intent intent) { | ||
updateWifiSwitch(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
|
||
IntentFilter f = new IntentFilter(); | ||
f.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); | ||
f.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); | ||
f.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); | ||
registerReceiver(receiver, f); | ||
|
||
updateTelnetSwitch(); | ||
updateWifiSwitch(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
unregisterReceiver(receiver); | ||
} | ||
|
||
protected void updateTelnetSwitch() { | ||
boolean telnetEnabled = isTelnetEnabled(); | ||
telnetSwitch.setChecked(telnetEnabled); | ||
telnetSwitch.setSummary(telnetEnabled ? "telnetd running on port 23" : "telnetd disabled"); | ||
} | ||
|
||
protected void updateWifiSwitch() { | ||
boolean wifiEnabled = isWifiEnabled(); | ||
String summary; | ||
if (wifiEnabled) { | ||
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); | ||
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); | ||
if (networkInfo.isConnected()) { | ||
summary = "Connected to " + wifiInfo.getSSID() + " (IP: " + Formatter.formatIpAddress(wifiInfo.getIpAddress()) + ")"; | ||
} else { | ||
NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); | ||
switch (state) { | ||
case SCANNING: | ||
summary = "Scanning..."; | ||
break; | ||
case AUTHENTICATING: | ||
case CONNECTING: | ||
case OBTAINING_IPADDR: | ||
summary = "Connecting..."; | ||
break; | ||
default: | ||
summary = "Wifi enabled"; | ||
} | ||
} | ||
} else { | ||
summary = "Wifi disabled"; | ||
} | ||
wifiSwitch.setChecked(wifiEnabled); | ||
wifiSwitch.setSummary(summary); | ||
} | ||
|
||
public int getTelnetPid() { | ||
Logger.info("getTelnetPid", "listing running processes"); | ||
return Procfs.findProcess(telnetStartCommand); | ||
} | ||
|
||
public boolean isTelnetEnabled() { | ||
return getTelnetPid() != -1; | ||
} | ||
|
||
public void setTelnetEnabled(final boolean enabled) { | ||
try { | ||
Logger.info("setTelnetEnabled", "setting telnetd to " + enabled); | ||
if (enabled) { | ||
Shell.exec(TextUtils.join(" ", telnetStartCommand)); | ||
} else { | ||
int pid = getTelnetPid(); | ||
if (pid != -1) | ||
Shell.exec("kill " + pid); | ||
} | ||
|
||
Condition.waitFor(new Condition.Runnable() { | ||
@Override | ||
public boolean run() { | ||
return isTelnetEnabled() == enabled; | ||
} | ||
}, 500, 2000); | ||
Logger.info("setTelnetEnabled", "done"); | ||
} catch (Exception e) { | ||
Logger.error("setTelnetEnabled", e); | ||
showError(e); | ||
} | ||
|
||
updateTelnetSwitch(); | ||
} | ||
|
||
public boolean isWifiEnabled() { | ||
int state = wifiManager.getWifiState(); | ||
return state == WifiManager.WIFI_STATE_ENABLING || state == WifiManager.WIFI_STATE_ENABLED; | ||
} | ||
|
||
public void setWifiEnabled(boolean enabled) { | ||
Logger.info("setWifiEnabled", "setting wifi to " + enabled); | ||
wifiManager.setWifiEnabled(enabled); | ||
} | ||
|
||
@Override | ||
public void onCheckedChanged(SwitchView view, boolean checked) { | ||
if (telnetSwitch.equals(view)) | ||
setTelnetEnabled(checked); | ||
else if (wifiSwitch.equals(view)) | ||
setWifiEnabled(checked); | ||
} | ||
|
||
public void onSettingsButtonClicked(View view) { | ||
Logger.info("onSettingsButtonClicked", "starting wifi settings activity"); | ||
boolean wifiEnabled = isWifiEnabled(); | ||
setWifiEnabled(true); | ||
startActivityForResult(new Intent("com.sony.scalar.app.wifisettings.WifiSettings"), wifiEnabled ? 1 : 0); | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int wifiEnabled, int result, Intent intent) { | ||
Logger.info("onActivityResult", "back from wifi settings activity"); | ||
super.onActivityResult(wifiEnabled, result, intent); | ||
setWifiEnabled(wifiEnabled == 1); | ||
} | ||
} |
This file contains 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
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/github/ma1co/openmemories/tweak/Procfs.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.github.ma1co.openmemories.tweak; | ||
|
||
import android.text.TextUtils; | ||
import android.util.SparseArray; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
|
||
public class Procfs { | ||
public static SparseArray<String> listProcesses() { | ||
SparseArray<String> processes = new SparseArray<String>(); | ||
|
||
for (File process : new File("/proc").listFiles()) { | ||
try { | ||
int pid = Integer.parseInt(process.getName()); | ||
byte[] buf = new byte[128]; | ||
int l = new FileInputStream(new File(process, "cmdline")).read(buf); | ||
if (l != -1) | ||
processes.put(pid, new String(buf, 0, l)); | ||
} catch (NumberFormatException e) { | ||
} catch (IOException e) { | ||
} | ||
} | ||
|
||
return processes; | ||
} | ||
|
||
public static int findProcess(String[] cmd) { | ||
String cmdline = TextUtils.join("\00", cmd) + "\00"; | ||
SparseArray<String> processes = Procfs.listProcesses(); | ||
for (int i = 0; i < processes.size(); i++) { | ||
if (cmdline.equals(processes.valueAt(i))) | ||
return processes.keyAt(i); | ||
} | ||
return -1; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/github/ma1co/openmemories/tweak/SwitchView.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.github.ma1co.openmemories.tweak; | ||
|
||
import android.content.Context; | ||
import android.content.res.TypedArray; | ||
import android.util.AttributeSet; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.widget.CheckBox; | ||
import android.widget.RelativeLayout; | ||
import android.widget.TextView; | ||
|
||
public class SwitchView extends RelativeLayout implements View.OnClickListener { | ||
public interface CheckedListener { | ||
void onCheckedChanged(SwitchView view, boolean checked); | ||
} | ||
|
||
private CheckedListener listener; | ||
private TextView titleView; | ||
private TextView summaryView; | ||
private CheckBox checkBox; | ||
|
||
public SwitchView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
setPadding(20, 10, 20, 10); | ||
|
||
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
inflater.inflate(R.layout.view_switch, this); | ||
titleView = (TextView) findViewById(R.id.title); | ||
summaryView = (TextView) findViewById(R.id.summary); | ||
checkBox = (CheckBox) findViewById(R.id.checkbox); | ||
|
||
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SwitchView, 0, 0); | ||
setTitle(ta.getString(R.styleable.SwitchView_title)); | ||
setSummary(ta.getString(R.styleable.SwitchView_summary)); | ||
ta.recycle(); | ||
|
||
checkBox.setOnClickListener(this); | ||
} | ||
|
||
public void setTitle(String title) { | ||
titleView.setText(title); | ||
} | ||
|
||
public void setSummary(String summary) { | ||
summaryView.setText(summary); | ||
} | ||
|
||
public boolean isChecked() { | ||
return checkBox.isChecked(); | ||
} | ||
|
||
public void setChecked(boolean checked) { | ||
checkBox.setChecked(checked); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
return checkBox.isEnabled(); | ||
} | ||
|
||
@Override | ||
public void setEnabled(boolean enabled) { | ||
checkBox.setEnabled(enabled); | ||
} | ||
|
||
public void setListener(CheckedListener listener) { | ||
this.listener = listener; | ||
} | ||
|
||
@Override | ||
public void onClick(View view) { | ||
if (listener != null) | ||
listener.onCheckedChanged(this, checkBox.isChecked()); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tweak="http://schemas.android.com/apk/res-auto" | ||
android:orientation="vertical" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.github.ma1co.openmemories.tweak.SwitchView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/wifi_switch" | ||
tweak:title="Enable Wifi"/> | ||
|
||
<com.github.ma1co.openmemories.tweak.SwitchView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/telnet_switch" | ||
tweak:title="Enable Telnet"/> | ||
|
||
<Button | ||
android:layout_width="200sp" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="center" | ||
android:onClick="onSettingsButtonClicked" | ||
android:text="Wifi settings"/> | ||
|
||
</LinearLayout> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<merge xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:textStyle="bold" | ||
android:id="@+id/title"/> | ||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_below="@id/title" | ||
android:id="@+id/summary"/> | ||
<CheckBox | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_alignParentRight="true" | ||
android:layout_centerVertical="true" | ||
android:id="@+id/checkbox"/> | ||
</merge> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<declare-styleable name="SwitchView"> | ||
<attr name="title" format="string"/> | ||
<attr name="summary" format="string"/> | ||
</declare-styleable> | ||
</resources> |