1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.dialer.contacts.displaypreference; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.os.UserManager; 22 import android.preference.PreferenceManager; 23 import com.android.dialer.inject.ApplicationContext; 24 import javax.inject.Inject; 25 26 /** 27 * Implementation of {@link ContactDisplayPreferences} backed by a {@link SharedPreferences}. Can 28 * only be instantiated when the device is unlocked. 29 */ 30 public final class ContactDisplayPreferencesImpl implements ContactDisplayPreferences { 31 32 private final Context appContext; 33 private final SharedPreferences sharedPreferences; 34 private final String displayOrderKey; 35 private final String sortOrderKey; 36 37 @Inject ContactDisplayPreferencesImpl(@pplicationContext Context appContext)38 ContactDisplayPreferencesImpl(@ApplicationContext Context appContext) { 39 this.appContext = appContext; 40 // @Unencrypted preference would be a better choice, but Android Preference only supports the 41 // default file. Stub should be used instead when device is locked. 42 this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(appContext); 43 displayOrderKey = appContext.getString(R.string.display_options_view_names_as_key); 44 sortOrderKey = appContext.getString(R.string.display_options_sort_list_by_key); 45 } 46 47 @Override getDisplayOrder()48 public DisplayOrder getDisplayOrder() { 49 migrate(); 50 if (!sharedPreferences.contains(displayOrderKey)) { 51 return DisplayOrder.PRIMARY; 52 } 53 return DisplayOrder.fromValue(appContext, sharedPreferences.getString(displayOrderKey, null)); 54 } 55 56 @Override setDisplayOrder(DisplayOrder displayOrder)57 public void setDisplayOrder(DisplayOrder displayOrder) { 58 sharedPreferences.edit().putString(displayOrderKey, displayOrder.getValue(appContext)).apply(); 59 } 60 61 @Override getSortOrder()62 public SortOrder getSortOrder() { 63 migrate(); 64 if (!sharedPreferences.contains(sortOrderKey)) { 65 return SortOrder.BY_PRIMARY; 66 } 67 return SortOrder.fromValue(appContext, sharedPreferences.getString(sortOrderKey, null)); 68 } 69 70 @Override setSortOrder(SortOrder sortOrder)71 public void setSortOrder(SortOrder sortOrder) { 72 sharedPreferences.edit().putString(sortOrderKey, sortOrder.getValue(appContext)).apply(); 73 } 74 75 /** 76 * Moves the stored values to the standard location. 77 * 78 * <p>Usually preferences are stored in {@code package.name_preferences.xml}. However the old 79 * com.android.contacts.common.preference.ContactsPreferences stored it in {@code 80 * package.name.xml} which is incompatible with the regular {@link android.preference.Preference} 81 * widgets. 82 */ migrate()83 private void migrate() { 84 if (!appContext.getSystemService(UserManager.class).isUserUnlocked()) { 85 return; 86 } 87 SharedPreferences oldPreference = 88 appContext.getSharedPreferences(appContext.getPackageName(), Context.MODE_PRIVATE); 89 if (oldPreference.contains(displayOrderKey) || oldPreference.contains(sortOrderKey)) { 90 sharedPreferences 91 .edit() 92 .putString( 93 displayOrderKey, 94 translateLegacyDisplayOrder(oldPreference.getInt(displayOrderKey, 1))) 95 .putString(sortOrderKey, translateLegacySortOrder(oldPreference.getInt(sortOrderKey, 1))) 96 .apply(); 97 oldPreference.edit().remove(displayOrderKey).remove(sortOrderKey).apply(); 98 } 99 } 100 translateLegacyDisplayOrder(int legacyValue)101 private String translateLegacyDisplayOrder(int legacyValue) { 102 switch (legacyValue) { 103 case 2: 104 return DisplayOrder.ALTERNATIVE.getValue(appContext); 105 default: 106 return DisplayOrder.PRIMARY.getValue(appContext); 107 } 108 } 109 translateLegacySortOrder(int legacyValue)110 private String translateLegacySortOrder(int legacyValue) { 111 switch (legacyValue) { 112 case 2: 113 return SortOrder.BY_ALTERNATIVE.getValue(appContext); 114 default: 115 return SortOrder.BY_PRIMARY.getValue(appContext); 116 } 117 } 118 } 119