1 /* 2 * Copyright (C) 2018 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.car.settings.wifi; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.text.TextUtils; 23 24 import androidx.localbroadcastmanager.content.LocalBroadcastManager; 25 import androidx.preference.EditTextPreference; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.FragmentController; 29 import com.android.car.settings.common.PreferenceController; 30 31 /** Business logic for adding/displaying the network name. */ 32 public class NetworkNamePreferenceController extends PreferenceController<EditTextPreference> { 33 34 /** Action used in the {@link Intent} sent by the {@link LocalBroadcastManager}. */ 35 public static final String ACTION_NAME_CHANGE = 36 "com.android.car.settings.wifi.NameChangeAction"; 37 /** Key used to store the name of the network. */ 38 public static final String KEY_NETWORK_NAME = "network_name"; 39 NetworkNamePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)40 public NetworkNamePreferenceController(Context context, String preferenceKey, 41 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 42 super(context, preferenceKey, fragmentController, uxRestrictions); 43 } 44 45 @Override getPreferenceType()46 protected Class<EditTextPreference> getPreferenceType() { 47 return EditTextPreference.class; 48 } 49 50 @Override updateState(EditTextPreference preference)51 protected void updateState(EditTextPreference preference) { 52 preference.setSummary(TextUtils.isEmpty(preference.getText()) ? getContext().getString( 53 R.string.default_network_name_summary) : preference.getText()); 54 } 55 56 @Override handlePreferenceChanged(EditTextPreference preference, Object newValue)57 protected boolean handlePreferenceChanged(EditTextPreference preference, Object newValue) { 58 String name = newValue.toString(); 59 preference.setText(name); 60 notifyNameChange(name); 61 refreshUi(); 62 return true; 63 } 64 notifyNameChange(String newName)65 private void notifyNameChange(String newName) { 66 Intent intent = new Intent(ACTION_NAME_CHANGE); 67 intent.putExtra(KEY_NETWORK_NAME, newName); 68 LocalBroadcastManager.getInstance(getContext()).sendBroadcastSync(intent); 69 } 70 } 71