• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 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.contacts.preference;
18 
19 import android.content.res.Configuration;
20 import android.database.Cursor;
21 import android.os.Bundle;
22 import android.preference.PreferenceActivity;
23 import android.provider.ContactsContract.DisplayNameSources;
24 import android.provider.ContactsContract.ProviderStatus;
25 import androidx.annotation.LayoutRes;
26 import androidx.annotation.NonNull;
27 import androidx.annotation.StringRes;
28 import androidx.appcompat.app.ActionBar;
29 import androidx.appcompat.app.AppCompatDelegate;
30 import androidx.appcompat.widget.Toolbar;
31 import android.text.TextUtils;
32 import android.view.MenuInflater;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.view.ViewGroup;
36 
37 import com.android.contacts.R;
38 import com.android.contacts.editor.SelectAccountDialogFragment;
39 import com.android.contacts.interactions.ImportDialogFragment;
40 import com.android.contacts.list.ProviderStatusWatcher;
41 import com.android.contacts.model.account.AccountWithDataSet;
42 import com.android.contacts.preference.DisplayOptionsPreferenceFragment.ProfileListener;
43 import com.android.contacts.preference.DisplayOptionsPreferenceFragment.ProfileQuery;
44 import com.android.contacts.util.AccountSelectionUtil;
45 
46 /**
47  * Contacts settings.
48  */
49 public final class ContactsPreferenceActivity extends PreferenceActivity
50         implements ProfileListener, SelectAccountDialogFragment.Listener {
51 
52     private static final String TAG_ABOUT = "about_contacts";
53     private static final String TAG_DISPLAY_OPTIONS = "display_options";
54 
55     private String mNewLocalProfileExtra;
56     private boolean mAreContactsAvailable;
57 
58     private ProviderStatusWatcher mProviderStatusWatcher;
59 
60     private AppCompatDelegate mCompatDelegate;
61 
62     public static final String EXTRA_NEW_LOCAL_PROFILE = "newLocalProfile";
63 
64     @Override
onCreate(Bundle savedInstanceState)65     protected void onCreate(Bundle savedInstanceState) {
66         mCompatDelegate = AppCompatDelegate.create(this, null);
67 
68         super.onCreate(savedInstanceState);
69         mCompatDelegate.onCreate(savedInstanceState);
70 
71 
72         final ActionBar actionBar = mCompatDelegate.getSupportActionBar();
73         if (actionBar != null) {
74             actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
75         }
76 
77         mProviderStatusWatcher = ProviderStatusWatcher.getInstance(this);
78 
79         mNewLocalProfileExtra = getIntent().getStringExtra(EXTRA_NEW_LOCAL_PROFILE);
80         final int providerStatus = mProviderStatusWatcher.getProviderStatus();
81         mAreContactsAvailable = providerStatus == ProviderStatus.STATUS_NORMAL;
82 
83         if (savedInstanceState == null) {
84             final DisplayOptionsPreferenceFragment fragment = DisplayOptionsPreferenceFragment
85                     .newInstance(mNewLocalProfileExtra, mAreContactsAvailable);
86             getFragmentManager().beginTransaction()
87                     .replace(android.R.id.content, fragment, TAG_DISPLAY_OPTIONS)
88                     .commit();
89             setActivityTitle(R.string.activity_title_settings);
90         } else {
91             final AboutPreferenceFragment aboutFragment = (AboutPreferenceFragment)
92                     getFragmentManager().findFragmentByTag(TAG_ABOUT);
93 
94             if (aboutFragment != null) {
95                 setActivityTitle(R.string.setting_about);
96             } else {
97                 setActivityTitle(R.string.activity_title_settings);
98             }
99         }
100     }
101 
102     @Override
onPostCreate(Bundle savedInstanceState)103     protected void onPostCreate(Bundle savedInstanceState) {
104         super.onPostCreate(savedInstanceState);
105         mCompatDelegate.onPostCreate(savedInstanceState);
106     }
107 
setSupportActionBar(Toolbar toolbar)108     public void setSupportActionBar(Toolbar toolbar) {
109         mCompatDelegate.setSupportActionBar(toolbar);
110     }
111 
112     @NonNull
113     @Override
getMenuInflater()114     public MenuInflater getMenuInflater() {
115         return mCompatDelegate.getMenuInflater();
116     }
117 
118     @Override
setContentView(@ayoutRes int layoutRes)119     public void setContentView(@LayoutRes int layoutRes) {
120         mCompatDelegate.setContentView(layoutRes);
121     }
122 
123     @Override
setContentView(View view)124     public void setContentView(View view) {
125         mCompatDelegate.setContentView(view);
126     }
127 
128     @Override
setContentView(View view, ViewGroup.LayoutParams params)129     public void setContentView(View view, ViewGroup.LayoutParams params) {
130         mCompatDelegate.setContentView(view, params);
131     }
132 
133     @Override
addContentView(View view, ViewGroup.LayoutParams params)134     public void addContentView(View view, ViewGroup.LayoutParams params) {
135         mCompatDelegate.addContentView(view, params);
136     }
137 
138     @Override
onPostResume()139     protected void onPostResume() {
140         super.onPostResume();
141         mCompatDelegate.onPostResume();
142     }
143 
144     @Override
onTitleChanged(CharSequence title, int color)145     protected void onTitleChanged(CharSequence title, int color) {
146         super.onTitleChanged(title, color);
147         mCompatDelegate.setTitle(title);
148     }
149 
150     @Override
onConfigurationChanged(Configuration newConfig)151     public void onConfigurationChanged(Configuration newConfig) {
152         super.onConfigurationChanged(newConfig);
153         mCompatDelegate.onConfigurationChanged(newConfig);
154     }
155 
156     @Override
onDestroy()157     protected void onDestroy() {
158         super.onDestroy();
159         mCompatDelegate.onDestroy();
160     }
161 
162     @Override
invalidateOptionsMenu()163     public void invalidateOptionsMenu() {
164         mCompatDelegate.invalidateOptionsMenu();
165     }
166 
showAboutFragment()167     protected void showAboutFragment() {
168         getFragmentManager().beginTransaction()
169                 .replace(android.R.id.content, AboutPreferenceFragment.newInstance(), TAG_ABOUT)
170                 .addToBackStack(null)
171                 .commit();
172         setActivityTitle(R.string.setting_about);
173     }
174 
175     @Override
onOptionsItemSelected(MenuItem item)176     public boolean onOptionsItemSelected(MenuItem item) {
177         if (item.getItemId() == android.R.id.home) {
178             onBackPressed();
179             return true;
180         }
181         return false;
182     }
183 
184     @Override
onBackPressed()185     public void onBackPressed() {
186         if (getFragmentManager().getBackStackEntryCount() > 0) {
187             setActivityTitle(R.string.activity_title_settings);
188             getFragmentManager().popBackStack();
189         } else {
190             super.onBackPressed();
191         }
192     }
193 
setActivityTitle(@tringRes int res)194     private void setActivityTitle(@StringRes int res) {
195         final ActionBar actionBar = mCompatDelegate.getSupportActionBar();
196         if (actionBar != null) {
197             actionBar.setTitle(res);
198         }
199     }
200 
201     @Override
onProfileLoaded(Cursor cursor)202     public void onProfileLoaded(Cursor cursor) {
203         boolean hasProfile = false;
204         String displayName = null;
205         long contactId = -1;
206         int displayNameSource = DisplayNameSources.UNDEFINED;
207         if (cursor != null && cursor.moveToFirst()) {
208             hasProfile = cursor.getInt(ProfileQuery.CONTACT_IS_USER_PROFILE) == 1;
209             displayName = cursor.getString(ProfileQuery.CONTACT_DISPLAY_NAME);
210             contactId = cursor.getLong(ProfileQuery.CONTACT_ID);
211             displayNameSource = cursor.getInt(ProfileQuery.DISPLAY_NAME_SOURCE);
212         }
213         if (hasProfile && TextUtils.isEmpty(displayName)) {
214             displayName = getString(R.string.missing_name);
215         }
216         final DisplayOptionsPreferenceFragment fragment = (DisplayOptionsPreferenceFragment)
217                 getFragmentManager().findFragmentByTag(TAG_DISPLAY_OPTIONS);
218         fragment.updateMyInfoPreference(hasProfile, displayName, contactId, displayNameSource);
219     }
220 
221     @Override
onAccountChosen(AccountWithDataSet account, Bundle extraArgs)222     public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
223         AccountSelectionUtil.doImport(this, extraArgs.getInt(ImportDialogFragment
224                 .KEY_RES_ID), account, extraArgs.getInt(ImportDialogFragment.KEY_SUBSCRIPTION_ID));
225     }
226 
227     @Override
onAccountSelectorCancelled()228     public void onAccountSelectorCancelled() {
229     }
230 }
231