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