diff --git a/src/com/tobykurien/batteryfu/data_switcher/LollipopSwitcher.java b/src/com/tobykurien/batteryfu/data_switcher/LollipopSwitcher.java index 406e7cf..649c7ef 100644 --- a/src/com/tobykurien/batteryfu/data_switcher/LollipopSwitcher.java +++ b/src/com/tobykurien/batteryfu/data_switcher/LollipopSwitcher.java @@ -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; + } } \ No newline at end of file diff --git a/src/com/tobykurien/batteryfu/data_switcher/MobileDataSwitcher.java b/src/com/tobykurien/batteryfu/data_switcher/MobileDataSwitcher.java index f638505..46d1a29 100755 --- a/src/com/tobykurien/batteryfu/data_switcher/MobileDataSwitcher.java +++ b/src/com/tobykurien/batteryfu/data_switcher/MobileDataSwitcher.java @@ -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); }