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;
18 
19 import android.support.annotation.MainThread;
20 import android.support.annotation.NonNull;
21 import android.support.annotation.Nullable;
22 import android.support.annotation.UiThread;
23 import android.util.ArraySet;
24 
25 import com.android.tv.data.Channel;
26 
27 import java.util.Set;
28 
29 /**
30  * A wrapper for safely getting the current {@link MainActivity}.
31  * Note that this class is not thread-safe. All the public methods should be called on main thread.
32  */
33 @MainThread
34 public final class MainActivityWrapper {
35     private MainActivity mActivity;
36 
37     private final Set<OnCurrentChannelChangeListener> mListeners = new ArraySet<>();
38 
39     /**
40      * Returns the current main activity.
41      * <b>WARNING</b> do not keep a reference to MainActivity, leaking activities is expensive.
42      */
getMainActivity()43     MainActivity getMainActivity() {
44         return mActivity;
45     }
46 
47     /**
48      * Checks if the given {@code activity} is the current main activity.
49      */
isCurrent(MainActivity activity)50     boolean isCurrent(MainActivity activity) {
51         return activity != null && mActivity == activity;
52     }
53 
54     /**
55      * Sets the currently created main activity instance.
56      */
57     @UiThread
onMainActivityCreated(@onNull MainActivity activity)58     public void onMainActivityCreated(@NonNull MainActivity activity) {
59         mActivity = activity;
60     }
61 
62     /**
63      * Unsets the main activity instance.
64      */
65     @UiThread
onMainActivityDestroyed(@onNull MainActivity activity)66     public void onMainActivityDestroyed(@NonNull MainActivity activity) {
67         if (mActivity != activity) {
68             mActivity = null;
69         }
70     }
71 
72     /**
73      * Notifies the current channel change.
74      */
notifyCurrentChannelChange(@onNull MainActivity caller, @Nullable Channel channel)75     void notifyCurrentChannelChange(@NonNull MainActivity caller, @Nullable Channel channel) {
76         if (mActivity == caller) {
77             for (OnCurrentChannelChangeListener listener : mListeners) {
78                 listener.onCurrentChannelChange(channel);
79             }
80         }
81     }
82 
83     /**
84      * Checks if the main activity is created.
85      */
isCreated()86     public boolean isCreated() {
87         return mActivity != null;
88     }
89 
90     /**
91      * Checks if the main activity is started.
92      */
isStarted()93     public boolean isStarted() {
94         return mActivity != null && mActivity.isActivityStarted();
95     }
96 
97     /**
98      * Checks if the main activity is resumed.
99      */
isResumed()100     public boolean isResumed() {
101         return mActivity != null && mActivity.isActivityResumed();
102     }
103 
104     /**
105      * Adds OnCurrentChannelChangeListener.
106      */
107     @UiThread
addOnCurrentChannelChangeListener(OnCurrentChannelChangeListener listener)108     public void addOnCurrentChannelChangeListener(OnCurrentChannelChangeListener listener) {
109         mListeners.add(listener);
110     }
111 
112     /**
113      * Removes OnCurrentChannelChangeListener.
114      */
115     @UiThread
removeOnCurrentChannelChangeListener(OnCurrentChannelChangeListener listener)116     public void removeOnCurrentChannelChangeListener(OnCurrentChannelChangeListener listener) {
117         mListeners.remove(listener);
118     }
119 
120     /**
121      * Listener for the current channel change in main activity.
122      */
123     public interface OnCurrentChannelChangeListener {
124         /**
125          * Called when the current channel changes.
126          */
onCurrentChannelChange(@ullable Channel channel)127         void onCurrentChannelChange(@Nullable Channel channel);
128     }
129 }
130