Home | History | Annotate | Download | only in telecom
      1 /*
      2  * Copyright (C) 2011 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.server.telecom;
     18 
     19 import android.app.ActionBar;
     20 import android.app.Activity;
     21 import android.content.Context;
     22 import android.content.SharedPreferences;
     23 import android.telecom.Log;
     24 import android.os.Bundle;
     25 import android.preference.EditTextPreference;
     26 import android.preference.Preference;
     27 import android.preference.PreferenceActivity;
     28 import android.preference.PreferenceScreen;
     29 import android.view.Menu;
     30 import android.view.MenuItem;
     31 
     32 // TODO: This class is newly copied into Telecom (com.android.server.telecom) from it previous
     33 // location in Telephony (com.android.phone). User's preferences stored in the old location
     34 // will be lost. We need code here to migrate KLP -> LMP settings values.
     35 
     36 /**
     37  * Settings activity to manage the responses available for the "Respond via SMS Message" feature to
     38  * respond to incoming calls.
     39  */
     40 public class RespondViaSmsSettings extends PreferenceActivity
     41         implements Preference.OnPreferenceChangeListener {
     42 
     43     private SharedPreferences mPrefs;
     44 
     45     @Override
     46     protected void onCreate(Bundle icicle) {
     47         super.onCreate(icicle);
     48         Log.d(this, "Settings: onCreate()...");
     49 
     50         // This function guarantees that QuickResponses will be in our
     51         // SharedPreferences with the proper values considering there may be
     52         // old QuickResponses in Telephony pre L.
     53         QuickResponseUtils.maybeMigrateLegacyQuickResponses(this);
     54 
     55         getPreferenceManager().setSharedPreferencesName(QuickResponseUtils.SHARED_PREFERENCES_NAME);
     56         mPrefs = getPreferenceManager().getSharedPreferences();
     57     }
     58 
     59     @Override
     60     public void onResume() {
     61         super.onResume();
     62 
     63         PreferenceScreen preferenceScreen = getPreferenceScreen();
     64         if (preferenceScreen != null) {
     65             preferenceScreen.removeAll();
     66         }
     67 
     68         // This preference screen is ultra-simple; it's just 4 plain
     69         // <EditTextPreference>s, one for each of the 4 "canned responses".
     70         //
     71         // The only nontrivial thing we do here is copy the text value of
     72         // each of those EditTextPreferences and use it as the preference's
     73         // "title" as well, so that the user will immediately see all 4
     74         // strings when they arrive here.
     75         //
     76         // Also, listen for change events (since we'll need to update the
     77         // title any time the user edits one of the strings.)
     78 
     79         addPreferencesFromResource(R.xml.respond_via_sms_settings);
     80         initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1));
     81         initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2));
     82         initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3));
     83         initPref(findPreference(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4));
     84 
     85         ActionBar actionBar = getActionBar();
     86         if (actionBar != null) {
     87             // android.R.id.home will be triggered in onOptionsItemSelected()
     88             actionBar.setDisplayHomeAsUpEnabled(true);
     89         }
     90     }
     91 
     92     // Preference.OnPreferenceChangeListener implementation
     93     @Override
     94     public boolean onPreferenceChange(Preference preference, Object newValue) {
     95         Log.d(this, "onPreferenceChange: key = %s", preference.getKey());
     96         Log.d(this, "  preference = '%s'", preference);
     97         Log.d(this, "  newValue = '%s'", newValue);
     98 
     99         EditTextPreference pref = (EditTextPreference) preference;
    100 
    101         // Copy the new text over to the title, just like in onCreate().
    102         // (Watch out: onPreferenceChange() is called *before* the
    103         // Preference itself gets updated, so we need to use newValue here
    104         // rather than pref.getText().)
    105         pref.setTitle((String) newValue);
    106 
    107         // Save the new preference value.
    108         SharedPreferences.Editor editor = mPrefs.edit();
    109         editor.putString(pref.getKey(), (String) newValue).commit();
    110 
    111         return true;  // means it's OK to update the state of the Preference with the new value
    112     }
    113 
    114     @Override
    115     public boolean onOptionsItemSelected(MenuItem item) {
    116         final int itemId = item.getItemId();
    117         switch (itemId) {
    118             case android.R.id.home:
    119                 goUpToTopLevelSetting(this);
    120                 return true;
    121             default:
    122         }
    123         return super.onOptionsItemSelected(item);
    124     }
    125 
    126     /**
    127      * Finish current Activity and go up to the top level Settings.
    128      */
    129     public static void goUpToTopLevelSetting(Activity activity) {
    130         activity.finish();
    131     }
    132 
    133     /**
    134      * Initialize the preference to the persisted preference value or default text.
    135      */
    136     private void initPref(Preference preference) {
    137         EditTextPreference pref = (EditTextPreference) preference;
    138         pref.setText(mPrefs.getString(pref.getKey(), pref.getText()));
    139         pref.setTitle(pref.getText());
    140         pref.setOnPreferenceChangeListener(this);
    141     }
    142 }
    143