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.tv.settings.connectivity; 18 19 import android.app.Activity; 20 import android.arch.lifecycle.ViewModelProviders; 21 import android.os.Bundle; 22 import android.os.Handler; 23 import android.os.Message; 24 import android.support.v4.app.Fragment; 25 import android.support.v4.app.FragmentActivity; 26 27 import com.android.tv.settings.R; 28 import com.android.tv.settings.connectivity.setup.MessageFragment; 29 import com.android.tv.settings.connectivity.util.State; 30 import com.android.tv.settings.connectivity.util.StateMachine; 31 32 /** 33 * State responsible for showing the save success page. 34 */ 35 public class SaveSuccessState implements State { 36 private final FragmentActivity mActivity; 37 private Fragment mFragment; 38 39 public SaveSuccessState(FragmentActivity activity) { 40 mActivity = activity; 41 } 42 43 @Override 44 public void processForward() { 45 mFragment = SaveSuccessFragment.newInstance( 46 mActivity.getString(R.string.wifi_setup_save_success)); 47 State.FragmentChangeListener listener = (FragmentChangeListener) mActivity; 48 listener.onFragmentChange(mFragment, true); 49 } 50 51 @Override 52 public void processBackward() { 53 StateMachine stateMachine = ViewModelProviders.of(mActivity).get(StateMachine.class); 54 stateMachine.back(); 55 } 56 57 @Override 58 public Fragment getFragment() { 59 return mFragment; 60 } 61 62 /** 63 * Fragment that shows network is successfully connected. 64 */ 65 public static class SaveSuccessFragment extends MessageFragment { 66 private static final int MSG_TIME_OUT = 1; 67 private static final int TIME_OUT_MS = 3 * 1000; 68 private static final String KEY_TIME_OUT_DURATION = "time_out_duration"; 69 private Handler mTimeoutHandler; 70 71 /** 72 * Get the fragment based on the title. 73 * 74 * @param title title of the fragment. 75 * @return the fragment. 76 */ 77 public static SaveSuccessFragment newInstance(String title) { 78 SaveSuccessFragment fragment = new SaveSuccessFragment(); 79 Bundle args = new Bundle(); 80 addArguments(args, title, false); 81 fragment.setArguments(args); 82 return fragment; 83 } 84 85 @Override 86 public void onCreate(Bundle savedInstanceState) { 87 StateMachine stateMachine = ViewModelProviders 88 .of(getActivity()) 89 .get(StateMachine.class); 90 mTimeoutHandler = new Handler() { 91 @Override 92 public void handleMessage(Message msg) { 93 switch (msg.what) { 94 case MSG_TIME_OUT: 95 stateMachine.finish(Activity.RESULT_OK); 96 break; 97 default: 98 break; 99 } 100 } 101 }; 102 super.onCreate(savedInstanceState); 103 } 104 105 @Override 106 public void onResume() { 107 super.onResume(); 108 mTimeoutHandler.sendEmptyMessageDelayed(MSG_TIME_OUT, 109 getArguments().getInt(KEY_TIME_OUT_DURATION, TIME_OUT_MS)); 110 } 111 112 @Override 113 public void onPause() { 114 super.onPause(); 115 mTimeoutHandler.removeMessages(MSG_TIME_OUT); 116 } 117 } 118 } 119