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
17package com.android.tv;
18
19import android.support.annotation.MainThread;
20import android.support.annotation.NonNull;
21import android.support.annotation.Nullable;
22import android.util.ArraySet;
23import com.android.tv.data.api.Channel;
24import 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
31public 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     */
40    MainActivity getMainActivity() {
41        return mActivity;
42    }
43
44    /** Checks if the given {@code activity} is the current main activity. */
45    boolean isCurrent(MainActivity activity) {
46        return activity != null && mActivity == activity;
47    }
48
49    /** Sets the currently created main activity instance. */
50    public void onMainActivityCreated(@NonNull MainActivity activity) {
51        mActivity = activity;
52    }
53
54    /** Unsets the main activity instance. */
55    public void onMainActivityDestroyed(@NonNull MainActivity activity) {
56        if (mActivity == activity) {
57            mActivity = null;
58        }
59    }
60
61    /** Notifies the current channel change. */
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. */
71    public boolean isCreated() {
72        return mActivity != null;
73    }
74
75    /** Checks if the main activity is started. */
76    public boolean isStarted() {
77        return mActivity != null && mActivity.isActivityStarted();
78    }
79
80    /** Checks if the main activity is resumed. */
81    public boolean isResumed() {
82        return mActivity != null && mActivity.isActivityResumed();
83    }
84
85    /** Adds OnCurrentChannelChangeListener. */
86    public void addOnCurrentChannelChangeListener(OnCurrentChannelChangeListener listener) {
87        mListeners.add(listener);
88    }
89
90    /** Removes OnCurrentChannelChangeListener. */
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. */
98        void onCurrentChannelChange(@Nullable Channel channel);
99    }
100}
101