Przeglądaj źródła

Add confirmation before exit

Earlier hitting the back button once caused exit from the app. Now
a toast message is displayed that prompts the user to hit the back
button twice to exit from the app. This prevents unintentional exit
in case the user hits the back button by chance.
arundhati24 7 lat temu
rodzic
commit
e24f46cde5

+ 67 - 45
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -40,6 +40,7 @@ import android.content.pm.PackageManager;
 import android.content.res.Resources.NotFoundException;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.Handler;
 import android.os.IBinder;
 import android.os.Parcelable;
 import android.support.design.widget.BottomNavigationView;
@@ -59,6 +60,7 @@ import android.view.View;
 import android.view.ViewTreeObserver;
 import android.widget.EditText;
 import android.widget.ImageView;
+import android.widget.Toast;
 
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
@@ -131,7 +133,7 @@ public class FileDisplayActivity extends HookActivity
         implements FileFragment.ContainerActivity,
         OnEnforceableRefreshListener, SortingOrderDialogFragment.OnSortingOrderListener,
         SendShareDialog.SendShareDialogDownloader {
- 
+
     private SyncBroadcastReceiver mSyncBroadcastReceiver;
     private UploadFinishReceiver mUploadFinishReceiver;
     private DownloadFinishReceiver mDownloadFinishReceiver;
@@ -166,13 +168,14 @@ public class FileDisplayActivity extends HookActivity
 
     private OCFile mWaitingToPreview;
 
+    private boolean mBackToExitPressedOnce = false;
     private boolean mSyncInProgress = false;
 
     private OCFile mWaitingToSend;
 
     private Collection<MenuItem> mDrawerMenuItemstoShowHideList;
 
-    private MediaServiceBinder mMediaServiceBinder =  null;
+    private MediaServiceBinder mMediaServiceBinder = null;
     private MediaServiceConnection mMediaServiceConnection = null;
 
     private String searchQuery;
@@ -1015,6 +1018,9 @@ public class FileDisplayActivity extends HookActivity
         }
     }
 
+    /**
+     * Prompt the user to press the back button twice in order to exit from the app
+     */
     @Override
     public void onBackPressed() {
         boolean isFabOpen = isFabOpen();
@@ -1029,45 +1035,59 @@ public class FileDisplayActivity extends HookActivity
          *    4. navigate up (only if drawer and FAB aren't open)
          */
 
-        if (isSearchOpen && searchView != null) {
-            searchView.setQuery("", true);
-            searchView.onActionViewCollapsed();
-            setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
-        } else if (isDrawerOpen && isFabOpen) {
-            // close drawer first
+        if (mBackToExitPressedOnce) {
             super.onBackPressed();
-        } else if (isDrawerOpen && !isFabOpen) {
-            // close drawer
-            super.onBackPressed();
-        } else if (!isDrawerOpen && isFabOpen) {
-            // close fab
-            getListOfFilesFragment().getFabMain().collapse();
-        } else {
-            // all closed
+            if (isSearchOpen && searchView != null) {
+                searchView.setQuery("", true);
+                searchView.onActionViewCollapsed();
+                setDrawerIndicatorEnabled(isDrawerIndicatorAvailable());
+            } else if (isDrawerOpen && isFabOpen) {
+                // close drawer first
+                super.onBackPressed();
+            } else if (isDrawerOpen && !isFabOpen) {
+                // close drawer
+                super.onBackPressed();
+            } else if (!isDrawerOpen && isFabOpen) {
+                // close fab
+                getListOfFilesFragment().getFabMain().collapse();
+            } else {
+                // all closed
 
-            //if PreviewImageActivity called this activity and mDualPane==false  then calls PreviewImageActivity again
-            if ((getIntent().getAction() != null && getIntent().getAction().equalsIgnoreCase(ACTION_DETAILS)) && !mDualPane) {
-                getIntent().setAction(null);
-                getIntent().putExtra(EXTRA_FILE, (OCFile) null);
-                startImagePreview(getFile(), false);
-            }
+                //if PreviewImageActivity called this activity and mDualPane==false  then calls PreviewImageActivity again
+                if ((getIntent().getAction() != null && getIntent().getAction().equalsIgnoreCase(ACTION_DETAILS)) && !mDualPane) {
+                    getIntent().setAction(null);
+                    getIntent().putExtra(EXTRA_FILE, (OCFile) null);
+                    startImagePreview(getFile(), false);
+                }
 
-            OCFileListFragment listOfFiles = getListOfFilesFragment();
-            if (mDualPane || getSecondFragment() == null) {
-                OCFile currentDir = getCurrentDir();
-                if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) {
-                    finish();
-                    return;
+                OCFileListFragment listOfFiles = getListOfFilesFragment();
+                if (mDualPane || getSecondFragment() == null) {
+                    OCFile currentDir = getCurrentDir();
+                    if (currentDir == null || currentDir.getParentId() == FileDataStorageManager.ROOT_PARENT_ID) {
+                        finish();
+                        return;
+                    }
+                    if (listOfFiles != null) {  // should never be null, indeed
+                        listOfFiles.onBrowseUp();
+                    }
                 }
                 if (listOfFiles != null) {  // should never be null, indeed
-                    listOfFiles.onBrowseUp();
+                    setFile(listOfFiles.getCurrentFile());
                 }
+                cleanSecondFragment();
             }
-            if (listOfFiles != null) {  // should never be null, indeed
-                setFile(listOfFiles.getCurrentFile());
-            }
-            cleanSecondFragment();
+            return;
         }
+        this.mBackToExitPressedOnce = true;
+        Toast.makeText(this, getString(R.string.toast_exit), Toast.LENGTH_SHORT)
+                .show();
+        new Handler().postDelayed(new Runnable() {
+
+            @Override
+            public void run() {
+                mBackToExitPressedOnce = false;
+            }
+        }, 2000);
     }
 
     @Override
@@ -1313,7 +1333,7 @@ public class FileDisplayActivity extends HookActivity
     private class UploadFinishReceiver extends BroadcastReceiver {
         /**
          * Once the file upload has finished -> update view
-         *
+         * <p>
          * {@link BroadcastReceiver} to enable upload feedback in UI
          */
         @Override
@@ -1591,11 +1611,13 @@ public class FileDisplayActivity extends HookActivity
         }
     }
 
-    private MediaServiceConnection newMediaConnection(){
+    private MediaServiceConnection newMediaConnection() {
         return new MediaServiceConnection();
     }
 
-    /** Defines callbacks for service binding, passed to bindService() */
+    /**
+     * Defines callbacks for service binding, passed to bindService()
+     */
     private class MediaServiceConnection implements ServiceConnection {
 
         @Override
@@ -1605,7 +1627,7 @@ public class FileDisplayActivity extends HookActivity
                 Log_OC.d(TAG, "Media service connected");
                 mMediaServiceBinder = (MediaServiceBinder) service;
 
-            }else {
+            } else {
                 return;
             }
 
@@ -1978,7 +2000,7 @@ public class FileDisplayActivity extends HookActivity
         } else {
             Log_OC.e(TAG, "Trying to send a NULL OCFile");
         }
-        
+
         mWaitingToSend = null;
     }
 
@@ -1987,7 +2009,7 @@ public class FileDisplayActivity extends HookActivity
      * to monitor the download progress and prepares the activity to send the file
      * when the download finishes.
      *
-     * @param file {@link OCFile} to download and preview.
+     * @param file         {@link OCFile} to download and preview.
      * @param packageName
      * @param activityName
      */
@@ -2182,15 +2204,15 @@ public class FileDisplayActivity extends HookActivity
         if (event.getIntent().getBooleanExtra(TEXT_PREVIEW, false)) {
             startTextPreview((OCFile) bundle.get(EXTRA_FILE), true);
         } else if (bundle.containsKey(PreviewVideoActivity.EXTRA_START_POSITION)) {
-            startMediaPreview((OCFile)bundle.get(EXTRA_FILE),
-                    (int)bundle.get(PreviewVideoActivity.EXTRA_START_POSITION),
-                    (boolean)bundle.get(PreviewVideoActivity.EXTRA_AUTOPLAY), true);
+            startMediaPreview((OCFile) bundle.get(EXTRA_FILE),
+                    (int) bundle.get(PreviewVideoActivity.EXTRA_START_POSITION),
+                    (boolean) bundle.get(PreviewVideoActivity.EXTRA_AUTOPLAY), true);
         } else if (bundle.containsKey(PreviewImageActivity.EXTRA_VIRTUAL_TYPE)) {
-            startImagePreview((OCFile)bundle.get(EXTRA_FILE),
-                    (VirtualFolderType)bundle.get(PreviewImageActivity.EXTRA_VIRTUAL_TYPE),
+            startImagePreview((OCFile) bundle.get(EXTRA_FILE),
+                    (VirtualFolderType) bundle.get(PreviewImageActivity.EXTRA_VIRTUAL_TYPE),
                     true);
         } else {
-            startImagePreview((OCFile)bundle.get(EXTRA_FILE),true);
+            startImagePreview((OCFile) bundle.get(EXTRA_FILE), true);
         }
     }
 

+ 2 - 0
src/main/res/values/strings.xml

@@ -770,4 +770,6 @@
     <string name="end_to_end_encryption_storing_keys">Storing keys</string>
     <string name="copy_move_to_encrypted_folder_not_supported">Copy/move into encrypted folder currently not supported.</string>
     <string name="untrusted_domain">Access through untrusted domain. Please see documentation for further info.</string>
+
+    <string name="toast_exit">Press again to exit</string>
 </resources>