Home | History | Annotate | Download | only in connectivity
      1 /*
      2  * Copyright (C) 2014 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 com.android.tv.settings.connectivity.setup.MessageWizardFragment;
     20 
     21 import android.app.Activity;
     22 import android.os.Bundle;
     23 import android.os.Handler;
     24 import android.os.Message;
     25 
     26 /**
     27  * Activity to wait a few seconds before returning
     28  */
     29 public class TimedMessageWizardFragment extends MessageWizardFragment {
     30 
     31     public interface Listener {
     32         void onTimedMessageCompleted();
     33     }
     34 
     35     private static final int MSG_TIME_OUT = 1;
     36     private static final int DEFAULT_TIME_OUT_MS = 3 * 1000;
     37     private static final String KEY_TIME_OUT_DURATION = "time_out_duration";
     38 
     39     public static TimedMessageWizardFragment newInstance(String title) {
     40         return newInstance(title, DEFAULT_TIME_OUT_MS);
     41     }
     42 
     43     public static TimedMessageWizardFragment newInstance(String title, int durationMs) {
     44         TimedMessageWizardFragment fragment = new TimedMessageWizardFragment();
     45         Bundle args = new Bundle();
     46         args.putInt(KEY_TIME_OUT_DURATION, durationMs);
     47         MessageWizardFragment.addArguments(args, title, false);
     48         fragment.setArguments(args);
     49         return fragment;
     50     }
     51 
     52 
     53     private Handler mTimeoutHandler;
     54     private Listener mListener;
     55 
     56     @Override
     57     public void onAttach(Activity activity) {
     58         if (activity instanceof Listener) {
     59             mListener = (Listener) activity;
     60         } else {
     61             throw new IllegalArgumentException("Activity must implement "
     62                     + "TimedMessageWizardFragment.Listener to use this fragment.");
     63         }
     64         super.onAttach(activity);
     65     }
     66 
     67     @Override
     68     public void onDetach() {
     69         super.onDetach();
     70         mListener = null;
     71     }
     72 
     73     @Override
     74     public void onCreate(Bundle savedInstanceState) {
     75         mTimeoutHandler = new Handler(getActivity().getMainLooper()) {
     76 
     77             @Override
     78             public void handleMessage(Message msg) {
     79                 switch (msg.what) {
     80                     case MSG_TIME_OUT:
     81                         if (mListener != null) {
     82                             mListener.onTimedMessageCompleted();
     83                         }
     84                         break;
     85                     default:
     86                         break;
     87                 }
     88             }
     89         };
     90         super.onCreate(savedInstanceState);
     91     }
     92 
     93     @Override
     94     public void onResume() {
     95         super.onResume();
     96         mTimeoutHandler.sendEmptyMessageDelayed(MSG_TIME_OUT,
     97                 getArguments().getInt(KEY_TIME_OUT_DURATION, DEFAULT_TIME_OUT_MS));
     98     }
     99 
    100     @Override
    101     public void onPause() {
    102         super.onPause();
    103         mTimeoutHandler.removeMessages(MSG_TIME_OUT);
    104     }
    105 }
    106