Skip to content

Commit

Permalink
Logic to request new storage permission
Browse files Browse the repository at this point in the history
  • Loading branch information
oaliaga committed Jun 7, 2022
1 parent 41d3bc2 commit da5fdd4
Show file tree
Hide file tree
Showing 14 changed files with 642 additions and 76 deletions.
12 changes: 11 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Expand Down Expand Up @@ -276,6 +276,13 @@
android:windowSoftInputMode="stateHidden|adjustResize"
android:configChanges="keyboardHidden|orientation"
/>
<activity
android:name="com.prey.activities.OpenSettingsActivity"
android:launchMode="singleInstance"
android:noHistory="true"
android:windowSoftInputMode="stateHidden|adjustResize"
android:configChanges="keyboardHidden|orientation"
/>

<!-- Exported added for android 12 -->
<receiver
Expand Down Expand Up @@ -434,6 +441,9 @@
android:exported="true"/>

<service android:name="com.prey.services.PreyAccessibilityService" />
<service android:name="com.prey.services.PreyStorageService" />
<service android:name="com.prey.services.PreyPermissionService" />
<service android:name="com.prey.services.PreyCloseNotificationService" />

<!-- Exported added for android 12 -->
<service
Expand Down
34 changes: 19 additions & 15 deletions app/src/main/java/com/prey/PreyApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public void onCreate() {
getApplicationContext().startActivity(intent);
}
} catch (Exception e) {
PreyLogger.e("Error call intent LoginActivity:"+e.getMessage(),e);
PreyLogger.e(String.format("Error call intent LoginActivity: %s", e.getMessage()), e);
}
run(this);
runReceiver(this);
try {
FirebaseApp.initializeApp(this);
} catch (Exception e) {
PreyLogger.e("Error FirebaseApp:" + e.getMessage(), e);
PreyLogger.e(String.format("Error FirebaseApp: %s", e.getMessage()), e);
}
PreyBetaController.startPrey(getApplicationContext());
}
Expand All @@ -79,32 +79,36 @@ public void run(final Context ctx) {
String deviceKey = PreyConfig.getPreyConfig(ctx).getDeviceId();
PreyConfig.getPreyConfig(ctx).setAwareDate("");
PreyConfig.getPreyConfig(ctx).initTimeC2dm();
PreyLogger.d("apiKey:" + apiKey);
PreyLogger.d("deviceKey:" + deviceKey);
PreyLogger.d("InstallationDate:" + PreyConfig.getPreyConfig(ctx).getInstallationDate());
PreyLogger.d(String.format("apiKey: %s", apiKey));
PreyLogger.d(String.format("deviceKey: %s", deviceKey));
PreyLogger.d(String.format("InstallationDate: %s", PreyConfig.getPreyConfig(ctx).getInstallationDate()));
if (PreyConfig.getPreyConfig(ctx).getInstallationDate() == 0) {
PreyConfig.getPreyConfig(ctx).setInstallationDate(new Date().getTime());
}
String sessionId = PreyUtils.randomAlphaNumeric(16);
PreyLogger.d("#######sessionId:" + sessionId);
PreyLogger.d(String.format("#######sessionId: %s", sessionId));
PreyConfig.getPreyConfig(ctx).setSessionId(sessionId);
final boolean missing = PreyConfig.getPreyConfig(ctx).isMissing();
PreyLogger.d("missing:" + missing);
PreyLogger.d(String.format("missing: %b", missing));
if (deviceKey != null && !"".equals(deviceKey)) {
new Thread() {
public void run() {
PreyConfig.getPreyConfig(ctx).registerC2dm();
PreyWebServices.getInstance().getProfile(ctx);
String initName = PreyWebServices.getInstance().getNameDevice(ctx);
if (initName != null && !"".equals(initName)) {
PreyLogger.d("initName:" + initName);
PreyLogger.d(String.format("initName: %s", initName));
PreyConfig.getPreyConfig(ctx).setDeviceName(initName);
}
PreyStatus.getInstance().initConfig(ctx);
boolean accessCoarseLocation=PreyPermission.canAccessCoarseLocation(ctx);
boolean accessFineLocation=PreyPermission.canAccessFineLocation(ctx);
boolean canAccessBackgroundLocation=PreyPermission.canAccessBackgroundLocation(ctx);
if((accessCoarseLocation||accessFineLocation)&&canAccessBackgroundLocation) {
boolean accessCoarseLocation = PreyPermission.canAccessCoarseLocation(ctx);
boolean accessFineLocation = PreyPermission.canAccessFineLocation(ctx);
boolean canAccessBackgroundLocation = PreyPermission.canAccessBackgroundLocation(ctx);
boolean verifyNotification = EventFactory.verifyNotification(ctx);
if (!verifyNotification) {
EventFactory.notification(ctx);
}
if ((accessCoarseLocation || accessFineLocation) && canAccessBackgroundLocation) {
GeofenceController.getInstance().run(ctx);
AwareController.getInstance().init(ctx);
}
Expand All @@ -122,7 +126,7 @@ public void run() {
AwareJobService.schedule(ctx);
}
} catch (Exception e) {
PreyLogger.e("error jobService.schedule : " + e.getMessage(), e);
PreyLogger.e(String.format("error jobService.schedule : %s", e.getMessage()), e);
}
if (PreyConfig.getPreyConfig(ctx).isRunBackground()) {
RunBackgroundCheckBoxPreference.notifyReady(ctx);
Expand All @@ -133,15 +137,15 @@ public void run() {
ctx.startService(new Intent(ctx, PreyDisablePowerOptionsService.class));
}
} catch (Exception e) {
PreyLogger.e("error startService PreyDisablePowerOptionsService : " + e.getMessage(), e);
PreyLogger.e(String.format("error startService PreyDisablePowerOptionsService : %s", e.getMessage()), e);
}
}
}
}
}.start();
}
} catch (Exception e) {
PreyLogger.e("Error PreyApp:" + e.getMessage(), e);
PreyLogger.e(String.format("Error PreyApp: %s", e.getMessage()), e);
}
}

Expand Down
125 changes: 117 additions & 8 deletions app/src/main/java/com/prey/PreyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ public class PreyConfig {
public static final String HELP_DIRECTORY = "preyHelp";
public static final String TIME_NEXT_ACCESSIBILITY = "TIME_NEXT_ACCESSIBILITY";
public static final String ACCESSIBILITY_DENIED = "ACCESSIBILITY_DENIED";
public static final String TIME_NEXT_ALLFILES = "TIME_NEXT_ALLFILES";
public static final String ALLFILES_DENIED = "ALLFILES_DENIED";
public static final String TIME_NEXT_LOCATIONBG = "TIME_NEXT_LOCATIONBG";
public static final String LOCATIONBG_DENIED = "LOCATIONBG_DENIED";

private boolean securityPrivilegesAlreadyPrompted;
private Context ctx;
public static String postUrl = null;
Expand Down Expand Up @@ -1385,29 +1390,133 @@ public void setViewSecure(boolean viewSecure){
saveBoolean(PreyConfig.VIEW_SECURE, viewSecure);
}

public void setTimeNextAccessibility(){
Calendar cal= Calendar.getInstance();
/**
* Method add a minute to request accessibility permission
*/
public void setTimeNextAccessibility() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MINUTE,1);
saveLong(TIME_NEXT_ACCESSIBILITY,cal.getTimeInMillis());
cal.add(Calendar.MINUTE, 1);
saveLong(TIME_NEXT_ACCESSIBILITY, cal.getTimeInMillis());
}

public boolean isTimeNextAccessibility(){
long timeLocationAware=getLong(TIME_NEXT_ACCESSIBILITY,0);
long timeNow=new Date().getTime();
if (timeNow<timeLocationAware){
/**
* Method that returns if it should request accessibility permission
*
* @return if you must ask
*/
public boolean isTimeNextAccessibility() {
long timeLocationAware = getLong(TIME_NEXT_ACCESSIBILITY, 0);
long timeNow = new Date().getTime();
if (timeNow < timeLocationAware) {
return true;
} else {
return false;
}
}

/**
* Method to deny accessibility permission
*
* @param denied
*/
public void setAccessibilityDenied(boolean denied) {
saveBoolean(PreyConfig.ACCESSIBILITY_DENIED, denied);
}

/**
* Method that gets whether to deny accessibility permission
*
* @return if you should deny
*/
public boolean getAccessibilityDenied() {
return getBoolean(PreyConfig.ACCESSIBILITY_DENIED, false);
}

/**
* Method add a minute to request storage permission
*/
public void setTimeNextAllFiles() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MINUTE, 1);
saveLong(TIME_NEXT_ALLFILES, cal.getTimeInMillis());
}

/**
* Method that returns if it should request storage permission
*
* @return if you must ask
*/
public boolean isTimeNextAllFiles() {
long timeLocationAllfiles = getLong(TIME_NEXT_ALLFILES, 0);
long timeNow = new Date().getTime();
if (timeNow < timeLocationAllfiles) {
return true;
} else {
return false;
}
}

/**
* Method to deny storage permission
*
* @param denied
*/
public void setAllFilesDenied(boolean denied) {
saveBoolean(PreyConfig.ALLFILES_DENIED, denied);
}

/**
* Method that gets whether to deny storage permission
*
* @return if you should deny
*/
public boolean getAllFilesDenied() {
return getBoolean(PreyConfig.ALLFILES_DENIED, false);
}

/**
* Method add a minute to request location background permission
*/
public void setTimeNextLocationBg() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MINUTE, 1);
saveLong(TIME_NEXT_LOCATIONBG, cal.getTimeInMillis());
}

/**
* Method that returns if it should request location background permission
*
* @return if you must ask
*/
public boolean isTimeNextLocationBg() {
long timeLocationBg = getLong(TIME_NEXT_LOCATIONBG, 0);
long timeNow = new Date().getTime();
if (timeNow < timeLocationBg) {
return true;
} else {
return false;
}
}

/**
* Method to deny location background permission
*
* @param denied
*/
public void setLocationBgDenied(boolean denied) {
saveBoolean(PreyConfig.LOCATIONBG_DENIED, denied);
}

/**
* Method that gets whether to deny location background permission
*
* @return if you should deny
*/
public boolean getLocationBgDenied() {
return getBoolean(PreyConfig.LOCATIONBG_DENIED, false);
}

}
73 changes: 73 additions & 0 deletions app/src/main/java/com/prey/PreyPermission.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;

import androidx.core.app.ActivityCompat;
Expand Down Expand Up @@ -117,6 +118,12 @@ public static boolean isAccessibilityServiceEnabled(Context context) {
return settingValue.indexOf("prey") > 0;
}

/**
* Method that validates if the accessibility method should request it
*
* @param ctx context
* @return true if accessibility method should request it, false otherwise
*/
public static boolean isAccessibilityServiceView(Context ctx) {
boolean isAccessibilityServiceEnabled = isAccessibilityServiceEnabled(ctx);
PreyLogger.d(String.format("isAccessibilityServiceEnabled:%s", isAccessibilityServiceEnabled));
Expand All @@ -134,4 +141,70 @@ public static boolean isAccessibilityServiceView(Context ctx) {
}
}
}

/**
* Method to obtain if storage is enabled
* @param ctx context
* @return true if storage enabled, false otherwise
*/
public static boolean isExternalStorageManager(Context ctx){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
return true;
}else{
return false;
}
}else{
return true;
}
}

/**
* Method that validates if the storage method should request it
*
* @param ctx context
* @return true if storage method should request it, false otherwise
*/
public static boolean isExternalStorageManagerView(Context ctx) {
boolean isExternalStorageManager = isExternalStorageManager(ctx);
PreyLogger.d(String.format("isExternalStorageManager:%s", isExternalStorageManager));
if (isExternalStorageManager) {
return isExternalStorageManager;
} else {
boolean allFilesDenied = PreyConfig.getPreyConfig(ctx).getAllFilesDenied();
PreyLogger.d(String.format("allFilesDenied:%s", allFilesDenied));
if (allFilesDenied) {
return allFilesDenied;
} else {
boolean isTimeNextAllFiles = PreyConfig.getPreyConfig(ctx).isTimeNextAllFiles();
PreyLogger.d(String.format("isTimeNextAllFiles:%s", isTimeNextAllFiles));
return isTimeNextAllFiles;
}
}
}

/**
* Method that validates if the background location method should request it
*
* @param ctx context
* @return true if background location method should request it, false otherwise
*/
public static boolean canAccessBackgroundLocationView(Context ctx) {
boolean canAccessBackgroundLocation = canAccessBackgroundLocation(ctx);
PreyLogger.d(String.format("canAccessBackgroundLocation:%s", canAccessBackgroundLocation));
if (canAccessBackgroundLocation) {
return canAccessBackgroundLocation;
} else {
boolean locatinBgDenied = PreyConfig.getPreyConfig(ctx).getLocationBgDenied();
PreyLogger.d(String.format("locatinBgDenied:%s", locatinBgDenied));
if (locatinBgDenied) {
return locatinBgDenied;
} else {
boolean isTimeNextLocationBg = PreyConfig.getPreyConfig(ctx).isTimeNextLocationBg();
PreyLogger.d(String.format("isTimeNextLocationBg:%s", isTimeNextLocationBg));
return isTimeNextLocationBg;
}
}
}

}
Loading

0 comments on commit da5fdd4

Please sign in to comment.