Home | History | Annotate | Download | only in setup
      1 /*
      2  * Copyright (C) 2017 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.connectivity.setup;
     18 
     19 import android.arch.lifecycle.ViewModelProviders;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.support.annotation.NonNull;
     23 import android.support.v17.leanback.widget.GuidanceStylist;
     24 import android.support.v17.leanback.widget.GuidedAction;
     25 import android.support.v17.leanback.widget.GuidedActionsStylist;
     26 import android.support.v4.app.Fragment;
     27 import android.support.v4.app.FragmentActivity;
     28 import android.text.TextUtils;
     29 import android.view.View;
     30 
     31 import com.android.tv.settings.R;
     32 import com.android.tv.settings.connectivity.WifiConfigHelper;
     33 import com.android.tv.settings.connectivity.util.State;
     34 import com.android.tv.settings.connectivity.util.StateMachine;
     35 
     36 import java.util.List;
     37 
     38 /**
     39  * State responsible for entering ssid.
     40  */
     41 public class EnterSsidState implements State {
     42     private final FragmentActivity mActivity;
     43     private Fragment mFragment;
     44 
     45     public EnterSsidState(FragmentActivity activity) {
     46         mActivity = activity;
     47     }
     48 
     49     @Override
     50     public void processForward() {
     51         mFragment = new EnterSsidFragment();
     52         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
     53         if (listener != null) {
     54             listener.onFragmentChange(mFragment, true);
     55         }
     56     }
     57 
     58     @Override
     59     public void processBackward() {
     60         mFragment = new EnterSsidFragment();
     61         FragmentChangeListener listener = (FragmentChangeListener) mActivity;
     62         if (listener != null) {
     63             listener.onFragmentChange(mFragment, false);
     64         }
     65     }
     66 
     67     @Override
     68     public Fragment getFragment() {
     69         return mFragment;
     70     }
     71 
     72     /**
     73      * Fragment that needs user to enter SSID of the network.
     74      */
     75     public static class EnterSsidFragment extends WifiConnectivityGuidedStepFragment {
     76         private UserChoiceInfo mUserChoiceInfo;
     77         private StateMachine mStateMachine;
     78         private GuidedAction mAction;
     79 
     80         @NonNull
     81         @Override
     82         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
     83             return new GuidanceStylist.Guidance(
     84                     getString(R.string.title_ssid),
     85                     null,
     86                     null,
     87                     null);
     88         }
     89 
     90         @Override
     91         public void onCreate(Bundle savedInstanceState) {
     92             mUserChoiceInfo = ViewModelProviders
     93                     .of(getActivity())
     94                     .get(UserChoiceInfo.class);
     95             mStateMachine = ViewModelProviders
     96                     .of(getActivity())
     97                     .get(StateMachine.class);
     98             super.onCreate(savedInstanceState);
     99         }
    100 
    101         @Override
    102         public GuidedActionsStylist onCreateActionsStylist() {
    103             return new GuidedActionsStylist() {
    104                 @Override
    105                 public int onProvideItemLayoutId() {
    106                     return R.layout.setup_text_input_item;
    107                 }
    108             };
    109         }
    110 
    111         @Override
    112         public void onCreateActions(@NonNull List<GuidedAction> actions,
    113                 Bundle savedInstanceState) {
    114             Context context = getActivity();
    115             CharSequence prevSsid = mUserChoiceInfo.getPageSummary(UserChoiceInfo.SSID);
    116             mAction = new GuidedAction.Builder(context)
    117                     .editable(true)
    118                     .title(prevSsid == null ? "" : prevSsid)
    119                     .build();
    120             actions.add(mAction);
    121         }
    122 
    123         @Override
    124         public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    125             openInEditMode(mAction);
    126         }
    127 
    128         @Override
    129         public long onGuidedActionEditedAndProceed(GuidedAction action) {
    130             mUserChoiceInfo.put(UserChoiceInfo.SSID, action.getTitle().toString());
    131             CharSequence ssid = action.getTitle();
    132             if (!TextUtils.equals(
    133                     mUserChoiceInfo.getWifiConfiguration().getPrintableSsid(), ssid)) {
    134                 mUserChoiceInfo.removePageSummary(UserChoiceInfo.PASSWORD);
    135             }
    136             WifiConfigHelper.setConfigSsid(mUserChoiceInfo.getWifiConfiguration(), ssid.toString());
    137             mStateMachine.getListener().onComplete(StateMachine.CONTINUE);
    138             return action.getId();
    139         }
    140     }
    141 }
    142