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.common.ui.setup;
18 
19 import android.app.Fragment;
20 import android.view.View;
21 import android.view.View.OnClickListener;
22 
23 /**
24  * Helper class for the execution in the fragment.
25  */
26 public class SetupActionHelper {
27     /**
28      * Executes the action of the given {@code actionId}.
29      */
onActionClick(Fragment fragment, String category, int actionId)30     public static void onActionClick(Fragment fragment, String category, int actionId) {
31         OnActionClickListener listener = null;
32         if (fragment instanceof SetupFragment) {
33             listener = ((SetupFragment) fragment).getOnActionClickListener();
34         }
35         if (listener == null && fragment.getActivity() instanceof OnActionClickListener) {
36             listener = (OnActionClickListener) fragment.getActivity();
37         }
38         if (listener != null) {
39             listener.onActionClick(category, actionId);
40         }
41     }
42 
43     /**
44      * Creates an {@link OnClickListener} to handle the action.
45      */
createOnClickListenerForAction(OnActionClickListener listener, String category, int actionId)46     public static OnClickListener createOnClickListenerForAction(OnActionClickListener listener,
47             String category, int actionId) {
48         return new OnActionClickListenerForAction(listener, category, actionId);
49     }
50 
51     private static class OnActionClickListenerForAction implements OnClickListener {
52         private final OnActionClickListener mListener;
53         private final String mCategory;
54         private final int mActionId;
55 
OnActionClickListenerForAction(OnActionClickListener listener, String category, int actionId)56         OnActionClickListenerForAction(OnActionClickListener listener, String category,
57                 int actionId) {
58             mListener = listener;
59             mCategory = category;
60             mActionId = actionId;
61         }
62 
63         @Override
onClick(View v)64         public void onClick(View v) {
65             if (mListener != null) {
66                 mListener.onActionClick(mCategory, mActionId);
67             }
68         }
69     }
70 
SetupActionHelper()71     private SetupActionHelper() { }
72 }
73