Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8a1c073 authored by c_yunong's avatar c_yunong Committed by Linux Build Service Account
Browse files

Contacts: Customize default storage behavior and storage position

- Add a new bool resource to customize contacts default storage
  behavior enable or not .
- Add a new integer resource to customize contacts default storage
  position.

Change-Id: Ib7e78e128649c279c2ae0b761f1e6089f2ca238e
CRs-Fixed: 1043367
parent 917e9b24
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (c) 2016, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<resources>

    <!-- Contacts storage default or not when have not only one writable account,default false -->
    <bool name="def_storage_behavior_enabled">false</bool>

    <!--
         store_contacts_position is less than 3,default 0 store contacts in phone,
            0:phone,1:sim1,2:sim2
    -->
    <integer name="def_storage_position" translatable="false">0</integer>

</resources>
+59 −1
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

@@ -46,7 +48,13 @@ import java.util.Set;
public class ContactEditorUtils {
    private static final String TAG = "ContactEditorUtils";

    private static final String KEY_DEFAULT_ACCOUNT = "ContactEditorUtils_default_account";
    private static final String KEY_KNOWN_ACCOUNTS = "ContactEditorUtils_known_accounts";
    // Key to tell the first time launch.
    private static final String KEY_ANYTHING_SAVED = "ContactEditorUtils_anything_saved";
    private static final int DEFAULT_STORAGE_PHONE = 0;
    private static final int DEFAULT_STORAGE_SIM_1 = 1;
    private static final int DEFAULT_STORAGE_SIM_2 = 2;

    private static final List<AccountWithDataSet> EMPTY_ACCOUNTS = ImmutableList.of();

@@ -141,6 +149,45 @@ public class ContactEditorUtils {
        editor.apply();
    }

    private AccountWithDataSet getOverlayDefualtAccount() {
        // if set the value of store contacts defalut
        if (mContext.getResources().getBoolean(R.bool.def_storage_behavior_enabled)) {
            List<AccountWithDataSet> accounts = getWritableAccounts();
            if (accounts != null && accounts.size() != 0) {
                String name = "";
                String type = "";
                // default Contacts storage postion
                int store_pos = mContext.getResources().getInteger(R.integer.def_storage_position);
                switch (store_pos) {
                    case DEFAULT_STORAGE_PHONE:
                        name = SimContactsConstants.PHONE_NAME;
                        type = SimContactsConstants.ACCOUNT_TYPE_PHONE;
                        break;
                    case DEFAULT_STORAGE_SIM_1:
                         TelephonyManager tm = (TelephonyManager) mContext.getSystemService(
                                 Context.TELEPHONY_SERVICE);
                        name = tm.getPhoneCount() > 1 ?
                                SimContactsConstants.SIM_NAME_1 : SimContactsConstants.SIM_NAME;
                        type = SimContactsConstants.ACCOUNT_TYPE_SIM;
                        break;
                    case DEFAULT_STORAGE_SIM_2:
                        name = SimContactsConstants.SIM_NAME_2;
                        type = SimContactsConstants.ACCOUNT_TYPE_SIM;
                        break;
                    default:
                        Log.e(TAG, "Bad default contacts storage position," +
                                " def_storage_position is " + store_pos);
                    break;
                }
                for (AccountWithDataSet account : accounts) {
                    if (name.equals(account.name) && type.equals(account.type)) {
                        return account;
                    }
                }
            }
        }
        return null;
    }
    /**
     * @return the default account saved with {@link #saveDefaultAndAllAccounts}.
     *
@@ -151,12 +198,18 @@ public class ContactEditorUtils {
     * Also note that the returned account may have been removed already.
     */
    public AccountWithDataSet getDefaultAccount() {

        AccountWithDataSet overlayDefaultAccount = getOverlayDefualtAccount();
        if (overlayDefaultAccount != null) {
            return overlayDefaultAccount;
        }

        final List<AccountWithDataSet> currentWritableAccounts = getWritableAccounts();
        if (currentWritableAccounts.size() == 1) {
            return currentWritableAccounts.get(0);
        }

        final String saved = mPrefs.getString(mDefaultAccountKey, null);
        final String saved = mPrefs.getString(KEY_DEFAULT_ACCOUNT, null);
        if (TextUtils.isEmpty(saved)) {
            return null;
        }
@@ -215,6 +268,11 @@ public class ContactEditorUtils {
     */
    @NeededForTesting
    public boolean shouldShowAccountChangedNotification() {
        // If default account is defined, accounts change should not show.
        final AccountWithDataSet overlayDefaultAccount = getOverlayDefualtAccount();
        if (overlayDefaultAccount != null) {
            return false;
        }
        if (isFirstLaunch()) {
            return true;
        }