Merge "Follow up for disabling creating archives in resources." into arc-apps
diff --git a/res/values/config.xml b/res/values/config.xml
index 74ca33c..a020583 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -19,7 +19,7 @@
     <string name="trusted_quick_viewer_package" translatable="false"></string>
 
     <bool name="show_documents_root">false</bool>
-    <bool name="enable_compressing">false</bool>
+    <bool name="enable_archive_creation">false</bool>
 
     <!-- Indicates if search view is taking the whole toolbar space. On larger
          layouts we reduce this to an input-box adjacent to menu actions. -->
diff --git a/src/com/android/documentsui/base/Shared.java b/src/com/android/documentsui/base/Shared.java
index a3634e7..fa228d1 100644
--- a/src/com/android/documentsui/base/Shared.java
+++ b/src/com/android/documentsui/base/Shared.java
@@ -251,13 +251,6 @@
         return context.getResources().getBoolean(R.bool.show_documents_root);
     }
 
-    /**
-     * Returns true if compressing is enabled.
-     */
-    public static boolean isCompressingEnabled(Context context) {
-        return context.getResources().getBoolean(R.bool.enable_compressing);
-    }
-
     /*
      * Returns true if the local/device storage root must be visible (this also hides
      * the option to toggle visibility in the menu.)
diff --git a/src/com/android/documentsui/files/FilesActivity.java b/src/com/android/documentsui/files/FilesActivity.java
index c6b5b60..f5a9664 100644
--- a/src/com/android/documentsui/files/FilesActivity.java
+++ b/src/com/android/documentsui/files/FilesActivity.java
@@ -102,7 +102,7 @@
             getColor(R.color.accent_dark));
 
         mInjector.menuManager = new MenuManager(
-                this,
+                mInjector.prefs,
                 mSearchManager,
                 mState,
                 new DirectoryDetails(this) {
diff --git a/src/com/android/documentsui/files/MenuManager.java b/src/com/android/documentsui/files/MenuManager.java
index a75b38c..e24f33c 100644
--- a/src/com/android/documentsui/files/MenuManager.java
+++ b/src/com/android/documentsui/files/MenuManager.java
@@ -17,7 +17,6 @@
 package com.android.documentsui.files;
 
 import android.app.Fragment;
-import android.content.Context;
 import android.view.KeyEvent;
 import android.view.KeyboardShortcutGroup;
 import android.view.KeyboardShortcutInfo;
@@ -31,21 +30,22 @@
 import com.android.documentsui.base.RootInfo;
 import com.android.documentsui.base.Shared;
 import com.android.documentsui.base.State;
+import com.android.documentsui.prefs.ScopedPreferences;
 import com.android.documentsui.queries.SearchViewManager;
 
 import java.util.List;
 import java.util.function.IntFunction;
 
 public final class MenuManager extends com.android.documentsui.MenuManager {
-    private final Context mContext;
+    private final ScopedPreferences mPreferences;
 
     public MenuManager(
-            Context context,
+            ScopedPreferences preferences,
             SearchViewManager searchManager,
             State displayState,
             DirectoryDetails dirDetails) {
         super(searchManager, displayState, dirDetails);
-        mContext = context;
+        mPreferences = preferences;
     }
 
     @Override
@@ -180,7 +180,7 @@
     @Override
     protected void updateCompress(MenuItem compress, SelectionDetails selectionDetails) {
         final boolean readOnly = !mDirDetails.canCreateDoc();
-        compress.setVisible(Shared.isCompressingEnabled(mContext));
+        compress.setVisible(mPreferences.getEnableArchiveCreation());
         compress.setEnabled(!readOnly && !selectionDetails.containsPartialFiles() &&
                 !selectionDetails.canExtract());
     }
diff --git a/src/com/android/documentsui/prefs/ScopedPreferences.java b/src/com/android/documentsui/prefs/ScopedPreferences.java
index 51dbb1b..fd0f0fe 100644
--- a/src/com/android/documentsui/prefs/ScopedPreferences.java
+++ b/src/com/android/documentsui/prefs/ScopedPreferences.java
@@ -17,9 +17,12 @@
 
 import android.content.Context;
 import android.content.SharedPreferences;
+import android.content.res.Resources;
 import android.preference.PreferenceManager;
 import android.text.TextUtils;
 
+import com.android.documentsui.R;
+
 /**
  * Provides an interface (and runtime implementation) for preferences that are
  * scoped (presumably to an activity). This eliminates the need to pass
@@ -29,27 +32,34 @@
 public interface ScopedPreferences {
 
     static final String INCLUDE_DEVICE_ROOT = "includeDeviceRoot-";
+    static final String ENABLE_ARCHIVE_CREATION = "enableArchiveCreation-";
 
     boolean getShowDeviceRoot();
     void setShowDeviceRoot(boolean display);
 
+    boolean getEnableArchiveCreation();
+    void setEnableArchiveCreation(boolean enabled);
+
     /**
      * @param scope An arbitrary string representitive of the scope
      *        for prefs that are set using this object.
      */
     public static ScopedPreferences create(Context context, String scope) {
-        return new RuntimeScopedPreferences(
+        return new RuntimeScopedPreferences(context.getResources(),
                 PreferenceManager.getDefaultSharedPreferences(context), scope);
     }
 
     static final class RuntimeScopedPreferences implements ScopedPreferences {
 
+        private Resources mResources;
         private SharedPreferences mSharedPrefs;
         private String mScope;
 
-        private RuntimeScopedPreferences(SharedPreferences sharedPrefs, String scope)  {
+        private RuntimeScopedPreferences(Resources resources, SharedPreferences sharedPrefs,
+                String scope)  {
             assert(!TextUtils.isEmpty(scope));
 
+            mResources = resources;
             mSharedPrefs = sharedPrefs;
             mScope = scope;
         }
@@ -63,6 +73,17 @@
         public void setShowDeviceRoot(boolean display) {
             mSharedPrefs.edit().putBoolean(INCLUDE_DEVICE_ROOT + mScope, display).apply();
         }
+
+        @Override
+        public boolean getEnableArchiveCreation() {
+            final boolean defaultValue = mResources.getBoolean(R.bool.enable_archive_creation);
+            return mSharedPrefs.getBoolean(ENABLE_ARCHIVE_CREATION + mScope, defaultValue);
+        }
+
+        @Override
+        public void setEnableArchiveCreation(boolean enabled) {
+            mSharedPrefs.edit().putBoolean(ENABLE_ARCHIVE_CREATION + mScope, enabled).apply();
+        }
     }
 
     static boolean shouldBackup(String s) {
diff --git a/tests/common/com/android/documentsui/testing/TestScopedPreferences.java b/tests/common/com/android/documentsui/testing/TestScopedPreferences.java
new file mode 100644
index 0000000..0b4f24e
--- /dev/null
+++ b/tests/common/com/android/documentsui/testing/TestScopedPreferences.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.documentsui.testing;
+
+import com.android.documentsui.prefs.ScopedPreferences;
+
+/**
+ * Test copy of ScopedPreferences, everything default to false.
+ */
+public class TestScopedPreferences implements ScopedPreferences {
+
+    private boolean mShowDeviceRoot;
+    private boolean mEnableArchiveCreation;
+
+    @Override
+    public boolean getShowDeviceRoot() {
+        return mShowDeviceRoot;
+    }
+
+    @Override
+    public void setShowDeviceRoot(boolean display) {
+        mShowDeviceRoot = display;
+    }
+
+    @Override
+    public boolean getEnableArchiveCreation() {
+        return mEnableArchiveCreation;
+    }
+
+    @Override
+    public void setEnableArchiveCreation(boolean enabled) {
+        mEnableArchiveCreation = enabled;
+    }
+ }
diff --git a/tests/unit/com/android/documentsui/files/MenuManagerTest.java b/tests/unit/com/android/documentsui/files/MenuManagerTest.java
index bf5d188..a60de01 100644
--- a/tests/unit/com/android/documentsui/files/MenuManagerTest.java
+++ b/tests/unit/com/android/documentsui/files/MenuManagerTest.java
@@ -33,6 +33,7 @@
 import com.android.documentsui.testing.TestMenu;
 import com.android.documentsui.testing.TestMenuInflater;
 import com.android.documentsui.testing.TestMenuItem;
+import com.android.documentsui.testing.TestScopedPreferences;
 import com.android.documentsui.testing.TestSearchViewManager;
 import com.android.documentsui.testing.TestSelectionDetails;
 
@@ -66,10 +67,10 @@
     private TestMenuItem advanced;
     private TestMenuItem eject;
 
-    private TestActivity testActivity;
     private TestSelectionDetails selectionDetails;
     private TestDirectoryDetails dirDetails;
     private TestSearchViewManager testSearchManager;
+    private TestScopedPreferences preferences;
     private RootInfo testRootInfo;
     private DocumentInfo testDocInfo;
     private State state = new State();
@@ -106,8 +107,8 @@
         selectionDetails = new TestSelectionDetails();
         dirDetails = new TestDirectoryDetails();
         testSearchManager = new TestSearchViewManager();
-        testActivity = TestActivity.create();
-        mgr = new MenuManager(testActivity, testSearchManager, state, dirDetails);
+        preferences = new TestScopedPreferences();
+        mgr = new MenuManager(preferences, testSearchManager, state, dirDetails);
 
         testRootInfo = new RootInfo();
         testDocInfo = new DocumentInfo();
@@ -115,7 +116,7 @@
 
     @Test
     public void testActionMenu() {
-        testActivity.resources.bools.put(R.bool.enable_compressing, true);
+        preferences.setEnableArchiveCreation(true);
         selectionDetails.canDelete = true;
         selectionDetails.canRename = true;
         dirDetails.canCreateDoc = true;
@@ -133,7 +134,7 @@
 
     @Test
     public void testActionMenu_containsPartial() {
-        testActivity.resources.bools.put(R.bool.enable_compressing, true);
+        preferences.setEnableArchiveCreation(true);
         selectionDetails.containPartial = true;
         dirDetails.canCreateDoc = true;
         mgr.updateActionMenu(testMenu, selectionDetails);
@@ -156,7 +157,7 @@
 
     @Test
     public void testActionMenu_compress() {
-        testActivity.resources.bools.put(R.bool.enable_compressing, true);
+        preferences.setEnableArchiveCreation(true);
         dirDetails.canCreateDoc = true;
         mgr.updateActionMenu(testMenu, selectionDetails);
 
@@ -165,7 +166,7 @@
 
     @Test
     public void testActionMenu_cantCompress() {
-        testActivity.resources.bools.put(R.bool.enable_compressing, true);
+        preferences.setEnableArchiveCreation(true);
         dirDetails.canCreateDoc = false;
         mgr.updateActionMenu(testMenu, selectionDetails);
 
@@ -209,7 +210,7 @@
 
     @Test
     public void testActionMenu_canExtract_hidesCopyToAndCompressAndShare() {
-        testActivity.resources.bools.put(R.bool.enable_compressing, true);
+        preferences.setEnableArchiveCreation(true);
         selectionDetails.canExtract = true;
         dirDetails.canCreateDoc = true;
         mgr.updateActionMenu(testMenu, selectionDetails);