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.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.util.State; 29 import com.android.tv.settings.connectivity.util.StateMachine; 30 31 /** 32 * State responsible for showing the connection successful page. 33 */ 34 public class SuccessState implements State { 35 private final FragmentActivity mActivity; 36 private Fragment mFragment; 37 38 public SuccessState(FragmentActivity activity) { 39 mActivity = activity; 40 } 41 42 @Override 43 public void processForward() { 44 mFragment = SuccessFragment.newInstance( 45 mActivity.getString(R.string.wifi_setup_connection_success)); 46 FragmentChangeListener listener = (FragmentChangeListener) mActivity; 47 if (listener != null) { 48 listener.onFragmentChange(mFragment, true); 49 } 50 } 51 52 @Override 53 public void processBackward() { 54 StateMachine stateMachine = ViewModelProviders.of(mActivity).get(StateMachine.class); 55 stateMachine.back(); 56 } 57 58 @Override 59 public Fragment getFragment() { 60 return mFragment; 61 } 62 63 /** 64 * Fragment that shows network is successfully connected. 65 */ 66 public static class SuccessFragment 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 */ 78 public static SuccessFragment newInstance(String title) { 79 SuccessFragment fragment = new SuccessFragment(); 80 Bundle args = new Bundle(); 81 addArguments(args, title, false); 82 fragment.setArguments(args); 83 return fragment; 84 } 85 86 @Override 87 public void onCreate(Bundle savedInstanceState) { 88 StateMachine stateMachine = ViewModelProviders 89 .of(getActivity()) 90 .get(StateMachine.class); 91 mTimeoutHandler = new Handler(getActivity().getMainLooper()) { 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 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 114 public void onPause() { 115 super.onPause(); 116 mTimeoutHandler.removeMessages(MSG_TIME_OUT); 117 } 118 } 119 } 120