From 3eb64bcdd745438391038a7ba7c1c8b29fc00516 Mon Sep 17 00:00:00 2001 From: "David A. Velasco" Date: Mon, 4 May 2015 10:52:57 +0200 Subject: [PATCH] When pass code is enabled, the app is sent to background if the screen is turned off and the windows are made secure to prevent they are rendered in the 'recents' menu --- src/com/owncloud/android/MainApp.java | 1 + .../authentication/PassCodeManager.java | 45 +++++++++++++------ .../ui/activity/FileDisplayActivity.java | 1 - 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/com/owncloud/android/MainApp.java b/src/com/owncloud/android/MainApp.java index 486ca65265..657469ef5e 100644 --- a/src/com/owncloud/android/MainApp.java +++ b/src/com/owncloud/android/MainApp.java @@ -88,6 +88,7 @@ public class MainApp extends Application { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log_OC.d(activity.getClass().getSimpleName(), "onCreate(Bundle) starting" ); + PassCodeManager.getPassCodeManager().onActivityCreated(activity); } @Override diff --git a/src/com/owncloud/android/authentication/PassCodeManager.java b/src/com/owncloud/android/authentication/PassCodeManager.java index ee1ba714e0..a49b2ab8ed 100644 --- a/src/com/owncloud/android/authentication/PassCodeManager.java +++ b/src/com/owncloud/android/authentication/PassCodeManager.java @@ -1,9 +1,13 @@ package com.owncloud.android.authentication; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.os.PowerManager; import android.preference.PreferenceManager; +import android.view.View; +import android.view.WindowManager; import com.owncloud.android.MainApp; import com.owncloud.android.ui.activity.PinCodeActivity; @@ -22,7 +26,7 @@ public class PassCodeManager { } private static int PASS_CODE_TIMEOUT = 1000; - // keeping a "low" value (not 0) is the easiest way to avoid prevent the pass code is requested on rotations + // keeping a "low" positive value is the easiest way to prevent the pass code is requested on rotations public static PassCodeManager mPassCodeManagerInstance = null; @@ -38,6 +42,14 @@ public class PassCodeManager { protected PassCodeManager() {}; + public void onActivityCreated(Activity activity) { + if (passCodeIsEnabled()) { + activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE); + } else { + activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE); + } + } + public void onActivityStarted(Activity activity) { if (!sExemptOfPasscodeActivites.contains(activity.getClass()) && passCodeShouldBeRequested() @@ -47,9 +59,10 @@ public class PassCodeManager { i.setAction(PinCodeActivity.ACTION_REQUEST); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); activity.startActivity(i); + } - mVisibleActivitiesCounter++; // AFTER passCodeShouldBeRequested was checked + mVisibleActivitiesCounter++; // keep it AFTER passCodeShouldBeRequested was checked } public void onActivityStopped(Activity activity) { @@ -57,22 +70,28 @@ public class PassCodeManager { mVisibleActivitiesCounter--; } setUnlockTimestamp(); - } - - private boolean passCodeShouldBeRequested(){ - if ((System.currentTimeMillis() - mTimestamp) > PASS_CODE_TIMEOUT && - mVisibleActivitiesCounter <= 0 - ){ - SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext()); - if (appPrefs.getBoolean("set_pincode", false)) { - return true; - } + PowerManager powerMgr = (PowerManager) activity.getSystemService(Context.POWER_SERVICE); + if (passCodeIsEnabled() && powerMgr != null && !powerMgr.isScreenOn()) { + activity.moveTaskToBack(true); } - return false; } private void setUnlockTimestamp() { mTimestamp = System.currentTimeMillis(); } + private boolean passCodeShouldBeRequested(){ + if ((System.currentTimeMillis() - mTimestamp) > PASS_CODE_TIMEOUT && + mVisibleActivitiesCounter <= 0 + ){ + return passCodeIsEnabled(); + } + return false; + } + + private boolean passCodeIsEnabled() { + SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(MainApp.getAppContext()); + return (appPrefs.getBoolean("set_pincode", false)); + } + } diff --git a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java index e09566081c..c9c8420e97 100644 --- a/src/com/owncloud/android/ui/activity/FileDisplayActivity.java +++ b/src/com/owncloud/android/ui/activity/FileDisplayActivity.java @@ -65,7 +65,6 @@ import com.actionbarsherlock.view.Window; import com.owncloud.android.BuildConfig; import com.owncloud.android.MainApp; import com.owncloud.android.R; -import com.owncloud.android.authentication.PassCodeManager; import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.files.services.FileDownloader; import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;