Skip to content

Commit

Permalink
fixed fallback to APNSwitcher when root access not acquired
Browse files Browse the repository at this point in the history
  • Loading branch information
tobykurien committed Mar 3, 2015
1 parent 04e13cd commit e40c58a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 123 deletions.
172 changes: 89 additions & 83 deletions src/com/tobykurien/batteryfu/data_switcher/LollipopSwitcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,100 +14,106 @@

/**
* A Lollipop-compatible data switcher
*
* @author andy
*/
public class LollipopSwitcher extends MobileDataSwitcher {
private ConnectivityManager connMan;
private Context context;
private ConnectivityManager connMan;
private Context context;

@Override
public void enableMobileData(Context context) {
@Override
public void enableMobileData(Context context) {

try {
// Preform su to get root priviledges
Process p = Runtime.getRuntime().exec("su");
try {
// Preform su to get root priviledges
Process p = Runtime.getRuntime().exec("su");

// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("svc data enable\n");
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
Log.d("BatteryFu", "LollipopSwitcher: Enabled Mobile Data");
}
else {
Log.d("BatteryFu", "LollipopSwitcher: Error enabling mobile data");
}
} catch (InterruptedException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error enabling mobile data");
}
} catch (IOException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error enabling mobile data");
}
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("svc data enable\n");
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
Log.d("BatteryFu", "LollipopSwitcher: Enabled Mobile Data");
} else {
Log.d("BatteryFu",
"LollipopSwitcher: Error enabling mobile data");
}
} catch (InterruptedException e) {
Log.d("BatteryFu",
"LollipopSwitcher: Error enabling mobile data");
}
} catch (IOException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error enabling mobile data");
}

}
}

@Override
public void disableMobileData(Context context) {
try {
// Preform su to get root privledges
Process p = Runtime.getRuntime().exec("su");
@Override
public void disableMobileData(Context context) {
try {
// Preform su to get root privledges
Process p = Runtime.getRuntime().exec("su");

// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("svc data disable\n");
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
Log.d("BatteryFu", "LollipopSwitcher: Disabled Mobile Data");
}
else {
Log.d("BatteryFu", "LollipopSwitcher: Error disabling mobile data");
}
} catch (InterruptedException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error disabling mobile data");
}
} catch (IOException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error disabling mobile data");
}
}
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("svc data disable\n");
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
Log.d("BatteryFu", "LollipopSwitcher: Disabled Mobile Data");
} else {
Log.d("BatteryFu",
"LollipopSwitcher: Error disabling mobile data");
}
} catch (InterruptedException e) {
Log.d("BatteryFu",
"LollipopSwitcher: Error disabling mobile data");
}
} catch (IOException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error disabling mobile data");
}
}

@Override
public int isToggleWorking(Context context) {
if(init(context))
return 0;
else
return R.string.need_root_text;
}
@Override
public int isToggleWorking(Context context) {
if (init(context))
return 0;
else
return R.string.need_root_text;
}

public boolean init(Context context) {
try {
// Perform su to get root priviledges
Process p = Runtime.getRuntime().exec("su");
public boolean init(Context context) {
try {
// Perform su to get root priviledges
Process p = Runtime.getRuntime().exec("su");

DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
Log.d("BatteryFu", "LollipopSwitcher: Successfully got root rights in init!");
return true;
}
else {
Log.d("BatteryFu", "LollipopSwitcher: Error gaining root access");
}
} catch (InterruptedException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error gaining root access");
}
} catch (IOException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error gaining root access");
}
return false;
}
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes("exit\n");
os.flush();

try {
p.waitFor();
if (p.exitValue() == 0) {
Log.d("BatteryFu",
"LollipopSwitcher: Successfully got root rights in init!");
return true;
} else {
Log.d("BatteryFu",
"LollipopSwitcher: Error gaining root access");
}
} catch (InterruptedException e) {
Log.d("BatteryFu",
"LollipopSwitcher: Error gaining root access");
}
} catch (IOException e) {
Log.d("BatteryFu", "LollipopSwitcher: Error gaining root access");
}
return false;
}

}
82 changes: 42 additions & 40 deletions src/com/tobykurien/batteryfu/data_switcher/MobileDataSwitcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,84 +10,86 @@
public abstract class MobileDataSwitcher {
public static MobileDataSwitcher apn = new APNSwitcher();
public static MobileDataSwitcher apndroid = new APNDroidSwitcher();
public static MobileDataSwitcher gb = new GingerbreadSwitcher();
public static MobileDataSwitcher ics = new ICSSwitcher();
public static MobileDataSwitcher lollipop = new LollipopSwitcher();
public static MobileDataSwitcher gb = new GingerbreadSwitcher();
public static MobileDataSwitcher ics = new ICSSwitcher();
public static MobileDataSwitcher lollipop = new LollipopSwitcher();

/**
* Get the right mobile data switcher
*
* @param pref
* @return
*/
public static MobileDataSwitcher getSwitcher(Context context, Settings settings) {
if(Build.VERSION.SDK_INT > 19) {
if(lollipop.isToggleWorking(context) == 0) {
//Toast.makeText(context, "Using Lollipop switcher", Toast.LENGTH_LONG).show();
return lollipop;
}
}
if ((Build.VERSION.SDK_INT >= 14) && (Build.VERSION.SDK_INT <= 19)) {
if (ics.isToggleWorking(context) == 0) {
//Toast.makeText(context, "Using ICS switcher", Toast.LENGTH_LONG).show();
return ics;
}
}

if ((Build.VERSION.SDK_INT >= 9) && (Build.VERSION.SDK_INT < 14)) {
if (gb.isToggleWorking(context) == 0) {
//Toast.makeText(context, "Using Gingerbread switcher", Toast.LENGTH_LONG).show();
return gb;
}
}

public static MobileDataSwitcher getSwitcher(Context context,
Settings settings) {
if (Build.VERSION.SDK_INT > 19) {
return lollipop;
}

if ((Build.VERSION.SDK_INT >= 14) && (Build.VERSION.SDK_INT <= 19)) {
return ics;
}

if ((Build.VERSION.SDK_INT >= 9) && (Build.VERSION.SDK_INT < 14)) {
return gb;
}

if (settings.isUseApndroid()) {
return apndroid;
} else {
return apn;
}
}

/**
* Used to restore all APN settings on startup to avoid conflicts between them
* Used to restore all APN settings on startup to avoid conflicts between
* them
*
* @param context
*/
public static void enableAll(Context context) {
if (apn == null) apn = new APNSwitcher();
if (apn == null)
apn = new APNSwitcher();
apn.enableMobileData(context);

if (apndroid == null) apndroid = new APNDroidSwitcher();

if (apndroid == null)
apndroid = new APNDroidSwitcher();
apndroid.enableMobileData(context);

if (gb == null) gb = new GingerbreadSwitcher();

if (gb == null)
gb = new GingerbreadSwitcher();
gb.enableMobileData(context);
}

public static void disableMobileData(Context context, Settings settings) {
getSwitcher(context, settings).disableMobileData(context);
}

public static void enableMobileData(Context context, Settings settings) {
getSwitcher(context, settings).enableMobileData(context);
}

/**
* Method to be implemented that will switch on mobile data
*
* @param context
*/
public abstract void enableMobileData(Context context);

/**
* Method to be implemented that will switch off mobile data
*
* @param context
*/
public abstract void disableMobileData(Context context);

/**
* Test to see if APN toggling will work. Returns the ID of the string message
* to display in the dialog box, or 0 if all is well. The id of the dialog title
* will be assumed to be return value - 1
* Test to see if APN toggling will work. Returns the ID of the string
* message to display in the dialog box, or 0 if all is well. The id of the
* dialog title will be assumed to be return value - 1
*
* @param context
* @return
*/
public abstract int isToggleWorking(Context context);
public abstract int isToggleWorking(Context context);
}

0 comments on commit e40c58a

Please sign in to comment.