• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2013 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 package com.android.dialer.settings;
17 
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.SharedPreferences;
21 import android.os.Bundle;
22 import android.os.UserManager;
23 import android.preference.PreferenceManager;
24 import android.provider.Settings;
25 import android.support.v4.os.BuildCompat;
26 import android.telecom.TelecomManager;
27 import android.telephony.TelephonyManager;
28 import android.view.MenuItem;
29 import android.widget.Toast;
30 
31 import com.android.contacts.common.compat.CompatUtils;
32 import com.android.contacts.common.compat.TelephonyManagerCompat;
33 import com.android.dialer.R;
34 import com.android.dialer.compat.FilteredNumberCompat;
35 import com.android.dialer.compat.SettingsCompat;
36 import com.android.dialer.compat.UserManagerCompat;
37 
38 import java.util.List;
39 
40 public class DialerSettingsActivity extends AppCompatPreferenceActivity {
41     protected SharedPreferences mPreferences;
42     private boolean migrationStatusOnBuildHeaders;
43 
44     @Override
onCreate(Bundle savedInstanceState)45     protected void onCreate(Bundle savedInstanceState) {
46         super.onCreate(savedInstanceState);
47         mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
48     }
49 
50     @Override
onResume()51     protected void onResume() {
52         super.onResume();
53         /*
54          * The headers need to be recreated if the migration status changed between when the headers
55          * were created and now.
56          */
57         if (migrationStatusOnBuildHeaders != FilteredNumberCompat.hasMigratedToNewBlocking()) {
58             invalidateHeaders();
59         }
60     }
61 
62     @Override
onBuildHeaders(List<Header> target)63     public void onBuildHeaders(List<Header> target) {
64         if (showDisplayOptions()) {
65             Header displayOptionsHeader = new Header();
66             displayOptionsHeader.titleRes = R.string.display_options_title;
67             displayOptionsHeader.fragment = DisplayOptionsSettingsFragment.class.getName();
68             target.add(displayOptionsHeader);
69         }
70 
71         Header soundSettingsHeader = new Header();
72         soundSettingsHeader.titleRes = R.string.sounds_and_vibration_title;
73         soundSettingsHeader.fragment = SoundSettingsFragment.class.getName();
74         soundSettingsHeader.id = R.id.settings_header_sounds_and_vibration;
75         target.add(soundSettingsHeader);
76 
77         if (CompatUtils.isMarshmallowCompatible()) {
78             Header quickResponseSettingsHeader = new Header();
79             Intent quickResponseSettingsIntent =
80                     new Intent(TelecomManager.ACTION_SHOW_RESPOND_VIA_SMS_SETTINGS);
81             quickResponseSettingsHeader.titleRes = R.string.respond_via_sms_setting_title;
82             quickResponseSettingsHeader.intent = quickResponseSettingsIntent;
83             target.add(quickResponseSettingsHeader);
84         }
85 
86         TelephonyManager telephonyManager =
87                 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
88 
89         // "Call Settings" (full settings) is shown if the current user is primary user and there
90         // is only one SIM. Before N, "Calling accounts" setting is shown if the current user is
91         // primary user and there are multiple SIMs. In N+, "Calling accounts" is shown whenever
92         // "Call Settings" is not shown.
93         boolean isPrimaryUser = isPrimaryUser();
94         if (isPrimaryUser
95                 && TelephonyManagerCompat.getPhoneCount(telephonyManager) <= 1) {
96             Header callSettingsHeader = new Header();
97             Intent callSettingsIntent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS);
98             callSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
99 
100             callSettingsHeader.titleRes = R.string.call_settings_label;
101             callSettingsHeader.intent = callSettingsIntent;
102             target.add(callSettingsHeader);
103         } else if (BuildCompat.isAtLeastN() || isPrimaryUser) {
104             Header phoneAccountSettingsHeader = new Header();
105             Intent phoneAccountSettingsIntent =
106                     new Intent(TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS);
107             phoneAccountSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
108 
109             phoneAccountSettingsHeader.titleRes = R.string.phone_account_settings_label;
110             phoneAccountSettingsHeader.intent = phoneAccountSettingsIntent;
111             target.add(phoneAccountSettingsHeader);
112         }
113         if (FilteredNumberCompat.canCurrentUserOpenBlockSettings(this)) {
114             Header blockedCallsHeader = new Header();
115             blockedCallsHeader.titleRes = R.string.manage_blocked_numbers_label;
116             blockedCallsHeader.intent = FilteredNumberCompat.createManageBlockedNumbersIntent(this);
117             target.add(blockedCallsHeader);
118             migrationStatusOnBuildHeaders = FilteredNumberCompat.hasMigratedToNewBlocking();
119         }
120         if (isPrimaryUser
121                 && (TelephonyManagerCompat.isTtyModeSupported(telephonyManager)
122                 || TelephonyManagerCompat.isHearingAidCompatibilitySupported(telephonyManager))) {
123             Header accessibilitySettingsHeader = new Header();
124             Intent accessibilitySettingsIntent =
125                     new Intent(TelecomManager.ACTION_SHOW_CALL_ACCESSIBILITY_SETTINGS);
126             accessibilitySettingsHeader.titleRes = R.string.accessibility_settings_title;
127             accessibilitySettingsHeader.intent = accessibilitySettingsIntent;
128             target.add(accessibilitySettingsHeader);
129         }
130     }
131 
132     /**
133     * Returns {@code true} or {@code false} based on whether the display options setting should be
134     * shown. For languages such as Chinese, Japanese, or Korean, display options aren't useful
135     * since contacts are sorted and displayed family name first by default.
136     *
137     * @return {@code true} if the display options should be shown, {@code false} otherwise.
138     */
showDisplayOptions()139     private boolean showDisplayOptions() {
140         return getResources().getBoolean(R.bool.config_display_order_user_changeable)
141                 && getResources().getBoolean(R.bool.config_sort_order_user_changeable);
142     }
143 
144     @Override
onHeaderClick(Header header, int position)145     public void onHeaderClick(Header header, int position) {
146         if (header.id == R.id.settings_header_sounds_and_vibration) {
147             // If we don't have the permission to write to system settings, go to system sound
148             // settings instead. Otherwise, perform the super implementation (which launches our
149             // own preference fragment.
150             if (!SettingsCompat.System.canWrite(this)) {
151                 Toast.makeText(
152                         this,
153                         getResources().getString(R.string.toast_cannot_write_system_settings),
154                         Toast.LENGTH_SHORT).show();
155                 startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS));
156                 return;
157             }
158         }
159         super.onHeaderClick(header, position);
160     }
161 
162     @Override
onOptionsItemSelected(MenuItem item)163     public boolean onOptionsItemSelected(MenuItem item) {
164         if (item.getItemId() == android.R.id.home) {
165             onBackPressed();
166             return true;
167         }
168         return false;
169     }
170 
171     @Override
onBackPressed()172     public void onBackPressed() {
173         if (!isSafeToCommitTransactions()) {
174             return;
175         }
176         super.onBackPressed();
177     }
178 
179     @Override
isValidFragment(String fragmentName)180     protected boolean isValidFragment(String fragmentName) {
181         return true;
182     }
183 
184     /**
185      * @return Whether the current user is the primary user.
186      */
isPrimaryUser()187     private boolean isPrimaryUser() {
188         return UserManagerCompat.isSystemUser((UserManager) getSystemService(Context.USER_SERVICE));
189     }
190 }
191