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