Home | History | Annotate | Download | only in name
      1 /*
      2  * Copyright (C) 2014 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.tv.settings.name;
     18 
     19 import android.app.Activity;
     20 import android.app.FragmentManager;
     21 import android.app.FragmentTransaction;
     22 import android.content.res.Resources;
     23 import android.os.Build;
     24 import android.os.Bundle;
     25 import android.text.TextUtils;
     26 import android.util.Log;
     27 import android.view.View;
     28 import android.widget.AdapterView;
     29 
     30 import java.util.ArrayList;
     31 
     32 import com.android.tv.settings.R;
     33 import com.android.tv.settings.connectivity.setup.TextInputWizardFragment;
     34 
     35 /**
     36  * Activity that displays Device Name settings
     37  */
     38 public class DeviceNameSettingsActivity extends Activity implements AdapterView.OnItemClickListener,
     39         TextInputWizardFragment.Listener {
     40 
     41     private static final String TAG = DeviceNameSettingsActivity.class.getSimpleName();
     42     private static final boolean DEBUG = false;
     43 
     44     public static final String EXTRA_SETUP_MODE = "in_setup_mode";
     45     private static final String INITIAL_STATE = "initial";
     46 
     47     private ArrayList<String> mOptions = new ArrayList<String>(2);
     48 
     49     @Override
     50     protected void onCreate(Bundle savedInstanceState) {
     51         super.onCreate(savedInstanceState);
     52         if (DEBUG) Log.v(TAG, "Starting Device Name activity");
     53         setContentView(R.layout.setup_activity);
     54 
     55         Resources res = getResources();
     56         mOptions.add(res.getString(R.string.keep_settings));
     57         mOptions.add(res.getString(R.string.change_setting));
     58         displaySummary();
     59     }
     60 
     61     @Override
     62     public void onBackPressed() {
     63         if (getFragmentManager().getBackStackEntryCount() == 1) {
     64             finish();
     65         } else {
     66             super.onBackPressed();
     67         }
     68     }
     69 
     70     @Override
     71     public boolean onTextInputComplete(String name) {
     72         setNameAndResetUi(name);
     73         return true;
     74     }
     75 
     76     @Override
     77     public void onItemClick(AdapterView<?> listView, View itemView, int position, long id) {
     78         if (position == 0) {
     79             // "Don't change" was selected, we're done!
     80             setResult(RESULT_OK);
     81             finish();
     82         } else {
     83             displaySetName();
     84         }
     85     }
     86 
     87     private void displaySummary() {
     88         DeviceNameSummaryFragment fragment = DeviceNameSummaryFragment.createInstance(
     89                 getResources().getString(R.string.settings_status_title),
     90                 getResources().getString(R.string.settings_status_description), mOptions);
     91         fragment.setOnItemClickListener(this);
     92         FragmentTransaction transaction = getFragmentManager().beginTransaction();
     93         transaction.replace(R.id.content_wrapper, fragment);
     94         transaction.addToBackStack(INITIAL_STATE);
     95         transaction.commit();
     96     }
     97 
     98     private void displaySetName() {
     99         FragmentTransaction transaction = getFragmentManager().beginTransaction();
    100         String[] rooms = getResources().getStringArray(R.array.rooms);
    101         ArrayList<String> roomsList = new ArrayList<String>();
    102         for (int ptr = 0; ptr < rooms.length; ptr++) {
    103             roomsList.add(rooms[ptr]);
    104         }
    105         SetDeviceNameFragment nameUi = SetDeviceNameFragment.createInstance(roomsList, true);
    106 
    107         nameUi.setListener(new SetDeviceNameListener() {
    108             @Override
    109             public boolean validateName(String name) {
    110                 return true;
    111             }
    112 
    113             @Override
    114             public void onDeviceNameSelected(String name) {
    115                 setNameAndResetUi(name);
    116             }
    117 
    118             @Override
    119             public void onCustomNameRequested() {
    120                 displayTextInputFragment();
    121             }
    122         });
    123 
    124         transaction.replace(R.id.content_wrapper, nameUi);
    125         transaction.addToBackStack(null);
    126         transaction.commit();
    127     }
    128 
    129     /**
    130      * Show the fragment that allows the user to give a custom name to their device
    131      */
    132     private void displayTextInputFragment() {
    133         // Magically TextInputWizardFragment hopes its enclosing activity is an instance of
    134         // its listener type, and we are so, onTextInputComplete(String) is automatically
    135         // called here
    136         String title = getString(R.string.select_title);
    137         String description = getString(R.string.select_description);
    138         TextInputWizardFragment customName = TextInputWizardFragment.newInstance(
    139                 TextUtils.expandTemplate(title, Build.MODEL).toString(),
    140                 TextUtils.expandTemplate(description, Build.MODEL).toString(),
    141                 TextInputWizardFragment.INPUT_TYPE_NORMAL,
    142                 null);
    143         FragmentTransaction transaction = getFragmentManager().beginTransaction();
    144         transaction.replace(R.id.content_wrapper, customName);
    145         transaction.addToBackStack(null);
    146         transaction.commit();
    147     }
    148 
    149     private void setNameAndResetUi(String name) {
    150         // if the name gets set, consider it success
    151         setResult(RESULT_OK);
    152         setDeviceName(name);
    153         // TODO delay reset until name update propagates
    154         getFragmentManager().popBackStack(INITIAL_STATE, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    155         finish();
    156     }
    157 
    158     private void setDeviceName(String name) {
    159         if (DEBUG) Log.v(TAG, String.format("Setting device name to %s", name));
    160         DeviceManager.setDeviceName(getApplicationContext(), name);
    161     }
    162 }
    163