Skip to content

Commit

Permalink
DeveloperActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
ma1co committed May 6, 2016
1 parent 22df38f commit 0191b18
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.github.ma1co.openmemories.tweak">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application android:label="@string/app_name" android:icon="@android:drawable/ic_menu_compass">
Expand All @@ -15,6 +18,7 @@
<activity android:name=".VideoActivity4k" android:theme="@style/BaseTheme" />
<activity android:name=".LanguageActivity" android:theme="@style/BaseTheme" />
<activity android:name=".ProtectionActivity" android:theme="@style/BaseTheme" />
<activity android:name=".DeveloperActivity" android:theme="@style/BaseTheme" />
</application>

</manifest>
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void uncaughtException(Thread thread, Throwable exp) {
} catch(NativeException e) { /* no op */ }
addTab("lang", "Languages", android.R.drawable.ic_menu_mapmode, LanguageActivity.class);
addTab("protection", "Protection", android.R.drawable.ic_lock_lock, ProtectionActivity.class);
addTab("developer", "Developer", android.R.drawable.ic_menu_manage, DeveloperActivity.class);
}

protected void addTab(String tag, String label, int iconId, Class activity) {
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/github/ma1co/openmemories/tweak/Procfs.java
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;
}
}
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());
}
}
27 changes: 27 additions & 0 deletions app/src/main/res/layout/activity_developer.xml
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>
19 changes: 19 additions & 0 deletions app/src/main/res/layout/view_switch.xml
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>
7 changes: 7 additions & 0 deletions app/src/main/res/values/attrs.xml
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>

0 comments on commit 0191b18

Please sign in to comment.