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