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.os.Bundle;
21 import android.util.Log;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 
25 /** Helper class for the execution in the fragment. */
26 public class SetupActionHelper {
27     private static final String TAG = "SetupActionHelper";
28 
29     /** Executes the action. */
onActionClick(Fragment fragment, String category, int actionId)30     public static boolean onActionClick(Fragment fragment, String category, int actionId) {
31         return onActionClick(fragment, category, actionId, null);
32     }
33 
34     /** Executes the action. */
onActionClick( Fragment fragment, String category, int actionId, Bundle params)35     public static boolean onActionClick(
36             Fragment fragment, String category, int actionId, Bundle params) {
37         if (fragment.getActivity() instanceof OnActionClickListener) {
38             return ((OnActionClickListener) fragment.getActivity())
39                     .onActionClick(category, actionId, params);
40         }
41         Log.e(
42                 TAG,
43                 "Activity can't handle the action: {category="
44                         + category
45                         + ", actionId="
46                         + actionId
47                         + ", params="
48                         + params
49                         + "}");
50         return false;
51     }
52 
53     /** Creates an {@link OnClickListener} to handle the action. */
createOnClickListenerForAction( Fragment fragment, String category, int actionId, Bundle params)54     public static OnClickListener createOnClickListenerForAction(
55             Fragment fragment, String category, int actionId, Bundle params) {
56         return new OnActionClickListenerForAction(fragment, category, actionId, params);
57     }
58 
59     /**
60      * The {@link OnClickListener} for the view.
61      *
62      * <p>Note that this class should be used only for the views in the {@code mFragment} to avoid
63      * the leak of mFragment.
64      */
65     private static class OnActionClickListenerForAction implements OnClickListener {
66         private final Fragment mFragment;
67         private final String mCategory;
68         private final int mActionId;
69         private final Bundle mParams;
70 
OnActionClickListenerForAction( Fragment fragment, String category, int actionId, Bundle params)71         OnActionClickListenerForAction(
72                 Fragment fragment, String category, int actionId, Bundle params) {
73             mFragment = fragment;
74             mCategory = category;
75             mActionId = actionId;
76             mParams = params;
77         }
78 
79         @Override
onClick(View v)80         public void onClick(View v) {
81             SetupActionHelper.onActionClick(mFragment, mCategory, mActionId, mParams);
82         }
83     }
84 
SetupActionHelper()85     private SetupActionHelper() {}
86 }
87