Skip to content

Commit

Permalink
Fixes issues with trying to get the settings file in processes that d…
Browse files Browse the repository at this point in the history
…on't have the READ_EXTERNAL_STORAGE.
  • Loading branch information
wardellbagby committed Nov 19, 2015
1 parent 637f44f commit 55792cf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 75 deletions.
10 changes: 5 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mrchandler.disableprox"
android:versionCode="4"
android:versionName="1.1">
android:versionCode="5"
android:versionName="1.1.1">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.android.vending.BILLING" />

<application
android:allowBackup="true"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:fullBackupContent="false">
android:theme="@style/AppTheme">
<meta-data
android:name="xposedmodule"
android:value="true" />
Expand All @@ -29,8 +29,8 @@
android:name=".ui.EditActivity"
android:exported="true"
android:label="@string/app_name"
android:uiOptions="splitActionBarWhenNarrow"
android:theme="@android:style/Theme.DeviceDefault.Dialog"
android:uiOptions="splitActionBarWhenNarrow"
android:windowSoftInputMode="adjustResize"
tools:ignore="ExportedActivity">

Expand Down
32 changes: 8 additions & 24 deletions app/src/main/java/com/mrchandler/disableprox/XposedMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.util.SparseArray;

import com.mrchandler.disableprox.util.Constants;
import com.mrchandler.disableprox.util.FileUtil;

import java.io.FileNotFoundException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;

Expand Down Expand Up @@ -177,30 +176,15 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
boolean getProximitySensorStatus() {
//False == Turn Off, True == Leave On
//Always assume that the user wants it disabled. They can disable the app if we fail somehow.
boolean proximitySensorStatus = false;
try {
Scanner scanner = new Scanner(FileUtil.getEnabledSettingsFile());
if (scanner.hasNextBoolean()) {
proximitySensorStatus = scanner.nextBoolean();
}
} catch (FileNotFoundException e) {
//We do nothing and assume that the proximity sensor should be disabled.
}
return proximitySensorStatus;
XposedHelpers.setStaticBooleanField(Environment.class, "sUserRequired", false);
XSharedPreferences sharedPreferences = new XSharedPreferences("com.mrchandler.disableprox");
return sharedPreferences.getBoolean(Constants.PREFS_KEY_PROX_SENSOR, false);

}

//TODO Implement a visual system for this.
//TODO Implement a visual system for this so users can pick which method.
int getMethodsUsedForDisabling() {
int enabledMethods = Constants.ENABLE_METHOD_1;
try {
Scanner scanner = new Scanner(FileUtil.getEnabledMethodsFile());
if (scanner.hasNextInt()) {
enabledMethods = scanner.nextInt();
}
} catch (FileNotFoundException e) {
//We do nothing and use the default method.
}
return enabledMethods;
return Constants.ENABLE_METHOD_1;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
Expand All @@ -24,11 +25,7 @@
import com.mrchandler.disableprox.bundle.BundleScrubber;
import com.mrchandler.disableprox.bundle.PluginBundleManager;
import com.mrchandler.disableprox.util.Constants;
import com.mrchandler.disableprox.util.FileUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Locale;

/**
Expand Down Expand Up @@ -71,15 +68,8 @@ public void onReceive(final Context context, final Intent intent) {
Log.e(Constants.LOG_TAG, "The Tasker Receiver does not have permission to write to storage.");

} else {
File settingsFile = FileUtil.getEnabledSettingsFile();
try {
FileWriter writer = new FileWriter(settingsFile);
writer.write(String.valueOf(setting));
writer.flush();
writer.close();
} catch (IOException e) {
//We can't save the setting like this but we know the file exist.
}
SharedPreferences prefs = context.getSharedPreferences("com.mrchandler.disableprox_preferences", Context.MODE_WORLD_READABLE);
prefs.edit().putBoolean(Constants.PREFS_KEY_PROX_SENSOR, setting).apply();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
Expand Down Expand Up @@ -77,7 +76,7 @@ protected void onCreate(final Bundle savedInstanceState) {
}
}
final IabHelper helper = new IabHelper(this, getString(R.string.google_billing_public_key));
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences prefs = getSharedPreferences("com.mrchandler.disableprox_preferences", MODE_WORLD_READABLE);
//Has the user purchased the Tasker IAP?
if (!prefs.getBoolean(Constants.PREFS_KEY_TASKER, false)) {
helper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
Expand All @@ -30,16 +29,10 @@

import com.mrchandler.disableprox.R;
import com.mrchandler.disableprox.util.Constants;
import com.mrchandler.disableprox.util.FileUtil;
import com.mrchandler.disableprox.util.IabHelper;
import com.mrchandler.disableprox.util.IabResult;
import com.mrchandler.disableprox.util.Inventory;

import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public final class SettingsActivity extends Activity {

private static final String TAG = SettingsActivity.class.getSimpleName();
Expand All @@ -55,16 +48,10 @@ public final class SettingsActivity extends Activity {
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefs = getSharedPreferences("com.mrchandler.disableprox_preferences", MODE_WORLD_READABLE);

if (null == savedInstanceState) {
try {
Scanner scanner = new Scanner(FileUtil.getEnabledSettingsFile());
if (scanner.hasNextBoolean()) {
((Switch) findViewById(android.R.id.checkbox)).setChecked(scanner.nextBoolean());
}
} catch (FileNotFoundException e) {
//Just don't set the checkbox.
Log.e(TAG, "Unable to get the settings.", e);
}
((Switch) findViewById(android.R.id.checkbox)).setChecked(prefs.getBoolean(Constants.PREFS_KEY_PROX_SENSOR, true));
}

int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Expand Down Expand Up @@ -95,8 +82,6 @@ public void onClick(DialogInterface dialog, int which) {
PERMISSION_RESULT_CODE);
}
}
prefs = PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);


freeloadTextView = (TextView) findViewById(R.id.freeload);
if (prefs.contains(Constants.PREFS_KEY_FREELOAD) && prefs.getBoolean(Constants.PREFS_KEY_FREELOAD, false)) {
Expand Down Expand Up @@ -164,15 +149,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override
public void finish() {
if (saveToFile) {
try {
FileWriter writer = new FileWriter(FileUtil.getEnabledSettingsFile());
writer.write(String.valueOf(((Switch) findViewById(android.R.id.checkbox)).isChecked()));
writer.flush();
writer.close();
} catch (IOException | SecurityException e) {
//Finish gracefully.
Log.e(TAG, "Unable to save settings to file.", e);
}
prefs.edit().putBoolean(Constants.PREFS_KEY_PROX_SENSOR, ((Switch) findViewById(android.R.id.checkbox)).isChecked()).apply();
}
super.finish();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final class Constants {
public static final String SKU_TASKER = "tasker_purchase";
public static final String PREFS_KEY_TASKER = "prefs_key_tasker";
public static final String PREFS_KEY_FREELOAD = "prefs_key_freeload";
public static final String PREFS_KEY_PROX_SENSOR = "prefs_key_prox_sensor";
public static final int ENABLE_METHOD_1 = 1;
public static final int ENABLE_METHOD_2 = 2;
public static final int ENABLE_METHOD_3 = 4;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ private FileUtil() {
}

public static File getEnabledSettingsFile() {
File file = new File(Environment.getExternalStorageDirectory() + PROX_FILE_NAME);
File file = new File(Environment.getExternalStorageDirectory() + PROX_FILES_DIRECTORY + PROX_FILE_NAME);
if (!file.exists()) {
new File(PROX_FILES_DIRECTORY).mkdirs();
new File(Environment.getExternalStorageDirectory() + PROX_FILES_DIRECTORY).mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
Expand All @@ -28,9 +28,9 @@ public static File getEnabledSettingsFile() {
}

public static File getEnabledMethodsFile() {
File file = new File(Environment.getExternalStorageDirectory() + PROX_SETTINGS_FILE_NAME);
File file = new File(Environment.getExternalStorageDirectory() + PROX_FILES_DIRECTORY + PROX_SETTINGS_FILE_NAME);
if (!file.exists()) {
new File(PROX_FILES_DIRECTORY).mkdirs();
new File(Environment.getExternalStorageDirectory() + PROX_FILES_DIRECTORY).mkdirs();
try {
file.createNewFile();
} catch (IOException e) {
Expand Down

0 comments on commit 55792cf

Please sign in to comment.