home: move clear history to settings

This patch moves the "clear history" button to the settings page, in
a new "privacy" category.
The home actionbar is cleaned and the "Scan QR Code" action has now
its own button with a new image.

Change-Id: I29407bdbdebcf0610920c465d422da721479ed96
Tuleap: #646
diff --git a/ring-android/app/src/main/java/cx/ring/fragments/SettingsFragment.java b/ring-android/app/src/main/java/cx/ring/fragments/SettingsFragment.java
index 8b7f935..2335454 100644
--- a/ring-android/app/src/main/java/cx/ring/fragments/SettingsFragment.java
+++ b/ring-android/app/src/main/java/cx/ring/fragments/SettingsFragment.java
@@ -21,13 +21,17 @@
 
 import android.Manifest;
 import android.app.Activity;
+import android.content.DialogInterface;
 import android.content.SharedPreferences;
 import android.content.pm.PackageManager;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.annotation.NonNull;
+import android.support.design.widget.Snackbar;
 import android.support.v14.preference.PreferenceFragment;
 import android.support.v14.preference.SwitchPreference;
+import android.support.v7.app.AlertDialog;
+import android.support.v7.preference.Preference;
 import android.text.TextUtils;
 import android.util.Log;
 import android.widget.Toast;
@@ -40,20 +44,41 @@
  * TODO: improvements : handle multiples permissions for feature.
  */
 public class SettingsFragment extends PreferenceFragment implements
-        SharedPreferences.OnSharedPreferenceChangeListener
-{
+        SharedPreferences.OnSharedPreferenceChangeListener {
     private static final String TAG = SettingsFragment.class.getSimpleName();
 
+    private LocalService.Callbacks mCallbacks = LocalService.DUMMY_CALLBACKS;
+
     private String FEATURE_KEY_PREF_CONTACTS = null;
     private String FEATURE_KEY_PREF_DIALER = null;
 
     @Override
+    public void onAttach(Activity activity) {
+        Log.d(TAG, "onAttach");
+        super.onAttach(activity);
+
+        if (!(activity instanceof LocalService.Callbacks)) {
+            throw new IllegalStateException("Activity must implement fragment's callbacks.");
+        }
+
+        mCallbacks = (LocalService.Callbacks) activity;
+    }
+
+    @Override
+    public void onDetach() {
+        Log.d(TAG, "onDetach");
+        super.onDetach();
+        mCallbacks = LocalService.DUMMY_CALLBACKS;
+    }
+
+    @Override
     public void onCreatePreferences(Bundle bundle, String s) {
         addPreferencesFromResource(R.xml.preferences);
         FEATURE_KEY_PREF_CONTACTS = getString(R.string.pref_systemContacts_key);
         FEATURE_KEY_PREF_DIALER = getString(R.string.pref_systemDialer_key);
     }
 
+    @Override
     public void onResume() {
         super.onResume();
         getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
@@ -71,6 +96,17 @@
     }
 
     @Override
+    public boolean onPreferenceTreeClick(Preference preference) {
+        if (null != preference) {
+            if (getString(R.string.pref_clearHistory_key).equals(preference.getKey())) {
+                this.clearHistory();
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
         String neededPermission = this.neededPermissionForFeature(key);
         this.handlePermissionsForFeaturePreference(sharedPreferences,
@@ -80,6 +116,7 @@
 
     /**
      * Check if all the features are in a good state of activation.
+     *
      * @see SettingsFragment#checkAndResolveCorrectSyncFeatureAndPermission(String)
      */
     private void checkAndResolveCorrectSync() {
@@ -90,6 +127,7 @@
     /**
      * Checks if a feature has the correct permission if any. If not, the feature is disable and the
      * layout reestablished to a proper state.
+     *
      * @param feature FEATURE_KEY_PREF_CONTACTS or FEATURE_KEY_PREF_DIALER
      */
     private void checkAndResolveCorrectSyncFeatureAndPermission(String feature) {
@@ -109,6 +147,7 @@
 
     /**
      * Provides the permission associated to a feature
+     *
      * @param feature FEATURE_KEY_PREF_CONTACTS or FEATURE_KEY_PREF_DIALER
      * @return the permission as a String
      */
@@ -116,8 +155,7 @@
         String neededPermission = null;
         if (FEATURE_KEY_PREF_CONTACTS.equals(feature)) {
             neededPermission = Manifest.permission.READ_CONTACTS;
-        }
-        else if (FEATURE_KEY_PREF_DIALER.equals(feature)) {
+        } else if (FEATURE_KEY_PREF_DIALER.equals(feature)) {
             neededPermission = Manifest.permission.WRITE_CALL_LOG;
         }
         return neededPermission;
@@ -125,9 +163,10 @@
 
     /**
      * Handles the permission managements for the key feature of the fragment
+     *
      * @param sharedPreferences Shared Preferences, such as those from onSharedPreferenceChanged
-     * @param feature FEATURE_KEY_PREF_CONTACTS or FEATURE_KEY_PREF_DIALER
-     * @param neededPermission if any, the permission to manage
+     * @param feature           FEATURE_KEY_PREF_CONTACTS or FEATURE_KEY_PREF_DIALER
+     * @param neededPermission  if any, the permission to manage
      */
     private void handlePermissionsForFeaturePreference(SharedPreferences sharedPreferences,
                                                        String feature,
@@ -135,7 +174,7 @@
         if (null == sharedPreferences ||
                 TextUtils.isEmpty(feature) ||
                 TextUtils.isEmpty(neededPermission)) {
-            Log.d(TAG,"No permission to handle for feature");
+            Log.d(TAG, "No permission to handle for feature");
             return;
         }
         //~ Checking if the user wants to use the feature
@@ -181,7 +220,8 @@
 
     /**
      * Enables or disables a feature
-     * @param enable boolean true if enabled, false otherwise
+     *
+     * @param enable  boolean true if enabled, false otherwise
      * @param feature FEATURE_KEY_PREF_CONTACTS or FEATURE_KEY_PREF_DIALER
      */
     private void enableFeature(boolean enable, String feature) {
@@ -199,14 +239,14 @@
 
     /**
      * Presents the right explanation toast for the denied permission of the corresponding feature
+     *
      * @param feature FEATURE_KEY_PREF_CONTACTS or FEATURE_KEY_PREF_DIALER
      */
     private void presentPermissionExplanationToastForFeature(String feature) {
         if (!TextUtils.isEmpty(feature)) {
             if (feature.equals(FEATURE_KEY_PREF_CONTACTS)) {
                 this.presentReadContactPermissionExplanationToast();
-            }
-            else if (feature.equals(FEATURE_KEY_PREF_DIALER)) {
+            } else if (feature.equals(FEATURE_KEY_PREF_DIALER)) {
                 this.presentWriteCallLogPermissionExplanationToast();
             }
         }
@@ -220,7 +260,7 @@
         Activity activity = getActivity();
         if (null != activity) {
             String toastMessage = getString(R.string.permission_dialog_read_contacts_message);
-            Toast.makeText(activity,toastMessage,Toast.LENGTH_LONG).show();
+            Toast.makeText(activity, toastMessage, Toast.LENGTH_LONG).show();
         }
     }
 
@@ -232,7 +272,40 @@
         Activity activity = getActivity();
         if (null != activity) {
             String toastMessage = getString(R.string.permission_dialog_write_call_log_message);
-            Toast.makeText(activity,toastMessage,Toast.LENGTH_LONG).show();
+            Toast.makeText(activity, toastMessage, Toast.LENGTH_LONG).show();
         }
     }
+
+    //region History
+
+    /**
+     * Clears all the conversations history. Ask the user to confirm first as it is a critical
+     * action.
+     */
+    private void clearHistory() {
+        final Activity activity = getActivity();
+        if (activity == null || getView() == null)
+            return;
+
+        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+        builder.setTitle(getString(R.string.clear_history_dialog_title))
+                .setMessage(getString(R.string.clear_history_dialog_message))
+                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                        mCallbacks.getService().clearHistory();
+                        Snackbar.make(getView(),
+                                getString(R.string.clear_history_completed),
+                                Snackbar.LENGTH_SHORT).show();
+                    }
+                })
+                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
+                    public void onClick(DialogInterface dialog, int id) {
+                        //~ Empty
+                    }
+                });
+        AlertDialog dialog = builder.create();
+        dialog.show();
+    }
+
+    //endregion
 }
diff --git a/ring-android/app/src/main/java/cx/ring/fragments/SmartListFragment.java b/ring-android/app/src/main/java/cx/ring/fragments/SmartListFragment.java
index 2019e86..45b74a1 100644
--- a/ring-android/app/src/main/java/cx/ring/fragments/SmartListFragment.java
+++ b/ring-android/app/src/main/java/cx/ring/fragments/SmartListFragment.java
@@ -170,7 +170,7 @@
 
     @Override
     public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
-        inflater.inflate(R.menu.call_list_menu, menu);
+        inflater.inflate(R.menu.smartlist_menu, menu);
         mSearchMenuItem = menu.findItem(R.id.menu_contact_search);
         mDialpadMenuItem = menu.findItem(R.id.menu_contact_dial);
         MenuItemCompat.setOnActionExpandListener(mSearchMenuItem, new MenuItemCompat.OnActionExpandListener() {
@@ -226,9 +226,6 @@
                 else
                     mSearchView.setInputType(EditorInfo.TYPE_CLASS_PHONE);
                 return true;
-            case R.id.menu_clear_history:
-                mCallbacks.getService().clearHistory();
-                return true;
             case R.id.menu_scan_qr:
                 QRCodeScannerActivity.startQRCodeScanWithFragmentReceiver(this);
             default:
diff --git a/ring-android/app/src/main/res/drawable-hdpi/ic_action_scan_qr.png b/ring-android/app/src/main/res/drawable-hdpi/ic_action_scan_qr.png
new file mode 100644
index 0000000..9da2c7e
--- /dev/null
+++ b/ring-android/app/src/main/res/drawable-hdpi/ic_action_scan_qr.png
Binary files differ
diff --git a/ring-android/app/src/main/res/drawable-mdpi/ic_action_scan_qr.png b/ring-android/app/src/main/res/drawable-mdpi/ic_action_scan_qr.png
new file mode 100644
index 0000000..7df4187
--- /dev/null
+++ b/ring-android/app/src/main/res/drawable-mdpi/ic_action_scan_qr.png
Binary files differ
diff --git a/ring-android/app/src/main/res/drawable-xhdpi/ic_action_scan_qr.png b/ring-android/app/src/main/res/drawable-xhdpi/ic_action_scan_qr.png
new file mode 100644
index 0000000..75c8419
--- /dev/null
+++ b/ring-android/app/src/main/res/drawable-xhdpi/ic_action_scan_qr.png
Binary files differ
diff --git a/ring-android/app/src/main/res/drawable-xxhdpi/ic_action_scan_qr.png b/ring-android/app/src/main/res/drawable-xxhdpi/ic_action_scan_qr.png
new file mode 100644
index 0000000..2815452
--- /dev/null
+++ b/ring-android/app/src/main/res/drawable-xxhdpi/ic_action_scan_qr.png
Binary files differ
diff --git a/ring-android/app/src/main/res/drawable-xxxhdpi/ic_action_scan_qr.png b/ring-android/app/src/main/res/drawable-xxxhdpi/ic_action_scan_qr.png
new file mode 100644
index 0000000..97c1df0
--- /dev/null
+++ b/ring-android/app/src/main/res/drawable-xxxhdpi/ic_action_scan_qr.png
Binary files differ
diff --git a/ring-android/app/src/main/res/menu/call_list_menu.xml b/ring-android/app/src/main/res/menu/smartlist_menu.xml
similarity index 75%
rename from ring-android/app/src/main/res/menu/call_list_menu.xml
rename to ring-android/app/src/main/res/menu/smartlist_menu.xml
index 73288a4..1be8a3f 100644
--- a/ring-android/app/src/main/res/menu/call_list_menu.xml
+++ b/ring-android/app/src/main/res/menu/smartlist_menu.xml
@@ -20,16 +20,16 @@
         android:visible="false"
         app:showAsAction="always" />
 
+    <!--
+    Created by Rohith M S from Noun Project
+    Licensed under Creative Commons :
+    http://creativecommons.org/licenses/by/3.0/us/legalcode
+    Used without any modification.
+    -->
     <item
         android:id="@+id/menu_scan_qr"
-        android:icon="@drawable/ic_action_remove"
+        android:icon="@drawable/ic_action_scan_qr"
         android:title="@string/scan_qr"
-        app:showAsAction="never" />
+        app:showAsAction="ifRoom" />
 
-    <item
-        android:id="@+id/menu_clear_history"
-        android:icon="@drawable/ic_action_remove"
-        android:orderInCategory="100"
-        android:title="@string/ab_clear_history"
-        app:showAsAction="never" />
 </menu>
\ No newline at end of file
diff --git a/ring-android/app/src/main/res/values/strings.xml b/ring-android/app/src/main/res/values/strings.xml
index f34ae62..db5f052 100644
--- a/ring-android/app/src/main/res/values/strings.xml
+++ b/ring-android/app/src/main/res/values/strings.xml
@@ -162,4 +162,9 @@
     <!-- QRCode Scan -->
     <string name="scan_qr_account_message">Scan the QR Code of the account you want to add.</string>
 
+    <!-- Settings -->
+    <string name="clear_history_dialog_title">Clear history ?</string>
+    <string name="clear_history_dialog_message">This action can not be undone.</string>
+    <string name="clear_history_completed">History has been cleared.</string>
+
 </resources>
diff --git a/ring-android/app/src/main/res/values/strings_preferences.xml b/ring-android/app/src/main/res/values/strings_preferences.xml
index 5ba5c81..0b6a545 100644
--- a/ring-android/app/src/main/res/values/strings_preferences.xml
+++ b/ring-android/app/src/main/res/values/strings_preferences.xml
@@ -3,6 +3,7 @@
     <string name="pref_category_network">Network</string>
     <string name="pref_category_contacts">Contacts</string>
     <string name="pref_category_system">System</string>
+    <string name="pref_category_privacy">Privacy</string>
 
     <string name="pref_mobileData_key" translatable="false">pref_mobileData</string>
     <string name="pref_mobileData_title">Mobile data</string>
@@ -23,4 +24,9 @@
     <string name="pref_startOnBoot_key" translatable="false">pref_startOnBoot</string>
     <string name="pref_startOnBoot_title">Start Ring on startup</string>
     <string name="pref_startOnBoot_summary">Run Ring in the background when the system starts.</string>
+
+    <string name="pref_clearHistory_key" translatable="false">pref_clearHistory</string>
+    <string name="pref_clearHistory_title">Clear history</string>
+    <string name="pref_clearHistory_summary">Clear all the conversations history. This action can not be undone.</string>
+
 </resources>
diff --git a/ring-android/app/src/main/res/xml/preferences.xml b/ring-android/app/src/main/res/xml/preferences.xml
index ff96cad..9f0118d 100644
--- a/ring-android/app/src/main/res/xml/preferences.xml
+++ b/ring-android/app/src/main/res/xml/preferences.xml
@@ -55,4 +55,11 @@
 
     </android.support.v7.preference.PreferenceCategory>
 
+    <android.support.v7.preference.PreferenceCategory android:title="@string/pref_category_privacy">
+        <Preference
+            android:key="@string/pref_clearHistory_key"
+            android:summary="@string/pref_clearHistory_summary"
+            android:title="@string/pref_clearHistory_title" />
+    </android.support.v7.preference.PreferenceCategory>
+
 </android.support.v7.preference.PreferenceScreen>
\ No newline at end of file