Home | History | Annotate | Download | only in dialog
      1 /*
      2  * Copyright (C) 2015 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.dialog;
     18 
     19 import android.app.Dialog;
     20 import android.app.DialogFragment;
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 import android.view.KeyEvent;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 
     27 import com.android.tv.MainActivity;
     28 import com.android.tv.R;
     29 
     30 /**
     31  * Dialog fragment with full screen.
     32  */
     33 public class FullscreenDialogFragment extends SafeDismissDialogFragment {
     34     public static final String DIALOG_TAG = FullscreenDialogFragment.class.getSimpleName();
     35     public static final String VIEW_LAYOUT_ID = "viewLayoutId";
     36     public static final String TRACKER_LABEL = "trackerLabel";
     37 
     38     /**
     39      * Creates a FullscreenDialogFragment. View class of viewLayoutResId should
     40      * implement {@link DialogView}.
     41      */
     42     public static FullscreenDialogFragment newInstance(int viewLayoutResId, String trackerLabel) {
     43         FullscreenDialogFragment f = new FullscreenDialogFragment();
     44         Bundle args = new Bundle();
     45         args.putInt(VIEW_LAYOUT_ID, viewLayoutResId);
     46         args.putString(TRACKER_LABEL, trackerLabel);
     47         f.setArguments(args);
     48         return f;
     49     }
     50 
     51     private String mTrackerLabel;
     52     private DialogView mDialogView;
     53 
     54     @Override
     55     public Dialog onCreateDialog(Bundle savedInstanceState) {
     56         FullscreenDialog dialog =
     57                 new FullscreenDialog(getActivity(), R.style.Theme_TV_dialog_Fullscreen);
     58         LayoutInflater inflater = LayoutInflater.from(getActivity());
     59         Bundle args = getArguments();
     60         mTrackerLabel = args.getString(TRACKER_LABEL);
     61         int viewLayoutResId = args.getInt(VIEW_LAYOUT_ID);
     62         View v = inflater.inflate(viewLayoutResId, null);
     63         dialog.setContentView(v);
     64         mDialogView = (DialogView) v;
     65         mDialogView.initialize((MainActivity) getActivity(), dialog);
     66         return dialog;
     67     }
     68 
     69     @Override
     70     public void onDestroy() {
     71         super.onDestroy();
     72         mDialogView.onDestroy();
     73     }
     74 
     75     @Override
     76     public String getTrackerLabel() {
     77         return mTrackerLabel;
     78     }
     79 
     80     private class FullscreenDialog extends Dialog {
     81         public FullscreenDialog(Context context, int theme) {
     82             super(context, theme);
     83         }
     84 
     85         @Override
     86         public void setContentView(View dialogView) {
     87             super.setContentView(dialogView);
     88             mDialogView = (DialogView) dialogView;
     89         }
     90 
     91         @Override
     92         public boolean dispatchKeyEvent(KeyEvent event) {
     93             boolean handled = super.dispatchKeyEvent(event);
     94             return handled || ((View) mDialogView).dispatchKeyEvent(event);
     95         }
     96 
     97         @Override
     98         public void onBackPressed() {
     99             mDialogView.onBackPressed();
    100         }
    101     }
    102 
    103     /**
    104      * Interface for the view of {@link FullscreenDialogFragment}.
    105      */
    106     public interface DialogView {
    107         /**
    108          * Called after the view is inflated and attached to the dialog.
    109          */
    110         void initialize(MainActivity activity, Dialog dialog);
    111         /**
    112          * Called when a back key is pressed.
    113          */
    114         void onBackPressed();
    115         /**
    116          * Called when {@link DialogFragment#onDestroy} is called.
    117          */
    118         void onDestroy();
    119     }
    120 }
    121