Home | History | Annotate | Download | only in settings
      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
     45     protected void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
     48     }
     49 
     50     @Override
     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
     63     public void onBuildHeaders(List<Header> target) {
     64         Header displayOptionsHeader = new Header();
     65         displayOptionsHeader.titleRes = R.string.display_options_title;
     66         displayOptionsHeader.fragment = DisplayOptionsSettingsFragment.class.getName();
     67         target.add(displayOptionsHeader);
     68 
     69         Header soundSettingsHeader = new Header();
     70         soundSettingsHeader.titleRes = R.string.sounds_and_vibration_title;
     71         soundSettingsHeader.fragment = SoundSettingsFragment.class.getName();
     72         soundSettingsHeader.id = R.id.settings_header_sounds_and_vibration;
     73         target.add(soundSettingsHeader);
     74 
     75         if (CompatUtils.isMarshmallowCompatible()) {
     76             Header quickResponseSettingsHeader = new Header();
     77             Intent quickResponseSettingsIntent =
     78                     new Intent(TelecomManager.ACTION_SHOW_RESPOND_VIA_SMS_SETTINGS);
     79             quickResponseSettingsHeader.titleRes = R.string.respond_via_sms_setting_title;
     80             quickResponseSettingsHeader.intent = quickResponseSettingsIntent;
     81             target.add(quickResponseSettingsHeader);
     82         }
     83 
     84         TelephonyManager telephonyManager =
     85                 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
     86 
     87         // "Call Settings" (full settings) is shown if the current user is primary user and there
     88         // is only one SIM. Before N, "Calling accounts" setting is shown if the current user is
     89         // primary user and there are multiple SIMs. In N+, "Calling accounts" is shown whenever
     90         // "Call Settings" is not shown.
     91         boolean isPrimaryUser = isPrimaryUser();
     92         if (isPrimaryUser
     93                 && TelephonyManagerCompat.getPhoneCount(telephonyManager) <= 1) {
     94             Header callSettingsHeader = new Header();
     95             Intent callSettingsIntent = new Intent(TelecomManager.ACTION_SHOW_CALL_SETTINGS);
     96             callSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     97 
     98             callSettingsHeader.titleRes = R.string.call_settings_label;
     99             callSettingsHeader.intent = callSettingsIntent;
    100             target.add(callSettingsHeader);
    101         } else if (BuildCompat.isAtLeastN() || isPrimaryUser) {
    102             Header phoneAccountSettingsHeader = new Header();
    103             Intent phoneAccountSettingsIntent =
    104                     new Intent(TelecomManager.ACTION_CHANGE_PHONE_ACCOUNTS);
    105             phoneAccountSettingsIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    106 
    107             phoneAccountSettingsHeader.titleRes = R.string.phone_account_settings_label;
    108             phoneAccountSettingsHeader.intent = phoneAccountSettingsIntent;
    109             target.add(phoneAccountSettingsHeader);
    110         }
    111         if (FilteredNumberCompat.canCurrentUserOpenBlockSettings(this)) {
    112             Header blockedCallsHeader = new Header();
    113             blockedCallsHeader.titleRes = R.string.manage_blocked_numbers_label;
    114             blockedCallsHeader.intent = FilteredNumberCompat.createManageBlockedNumbersIntent(this);
    115             target.add(blockedCallsHeader);
    116             migrationStatusOnBuildHeaders = FilteredNumberCompat.hasMigratedToNewBlocking();
    117         }
    118         if (isPrimaryUser
    119                 && (TelephonyManagerCompat.isTtyModeSupported(telephonyManager)
    120                 || TelephonyManagerCompat.isHearingAidCompatibilitySupported(telephonyManager))) {
    121             Header accessibilitySettingsHeader = new Header();
    122             Intent accessibilitySettingsIntent =
    123                     new Intent(TelecomManager.ACTION_SHOW_CALL_ACCESSIBILITY_SETTINGS);
    124             accessibilitySettingsHeader.titleRes = R.string.accessibility_settings_title;
    125             accessibilitySettingsHeader.intent = accessibilitySettingsIntent;
    126             target.add(accessibilitySettingsHeader);
    127         }
    128     }
    129 
    130     @Override
    131     public void onHeaderClick(Header header, int position) {
    132         if (header.id == R.id.settings_header_sounds_and_vibration) {
    133             // If we don't have the permission to write to system settings, go to system sound
    134             // settings instead. Otherwise, perform the super implementation (which launches our
    135             // own preference fragment.
    136             if (!SettingsCompat.System.canWrite(this)) {
    137                 Toast.makeText(
    138                         this,
    139                         getResources().getString(R.string.toast_cannot_write_system_settings),
    140                         Toast.LENGTH_SHORT).show();
    141                 startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS));
    142                 return;
    143             }
    144         }
    145         super.onHeaderClick(header, position);
    146     }
    147 
    148     @Override
    149     public boolean onOptionsItemSelected(MenuItem item) {
    150         if (item.getItemId() == android.R.id.home) {
    151             onBackPressed();
    152             return true;
    153         }
    154         return false;
    155     }
    156 
    157     @Override
    158     public void onBackPressed() {
    159         if (!isSafeToCommitTransactions()) {
    160             return;
    161         }
    162         super.onBackPressed();
    163     }
    164 
    165     @Override
    166     protected boolean isValidFragment(String fragmentName) {
    167         return true;
    168     }
    169 
    170     /**
    171      * @return Whether the current user is the primary user.
    172      */
    173     private boolean isPrimaryUser() {
    174         return UserManagerCompat.isSystemUser((UserManager) getSystemService(Context.USER_SERVICE));
    175     }
    176 }
    177