View Javadoc
1   /*
2    * Copyright (C) 2003-2015 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.base;
20  
21  import java.lang.ref.WeakReference;
22  import java.util.ArrayList;
23  import java.util.HashSet;
24  
25  import org.exoplatform.utils.Log;
26  
27  import android.support.v4.app.FragmentActivity;
28  
29  /**
30   * Created by The eXo Platform SAS Author : MinhTDH MinhTDH@exoplatform.com Jul
31   * 20, 2015
32   */
33  public abstract class BaseActivity extends FragmentActivity {
34    /**
35     * same purpose with ActivityLifecycleCallbacks in API 14, but only interested
36     * in onPaused and onResume.
37     * 
38     * @author MinhTDH
39     */
40    public interface ActivityLifecycleCallbacks {
41      void onResume(BaseActivity act);
42  
43      void onPause(BaseActivity act);
44    }
45  
46    public static class BasicActivityLifecycleCallbacks implements ActivityLifecycleCallbacks {
47  
48      @Override
49      public void onResume(BaseActivity act) {
50  
51      }
52  
53      @Override
54      public void onPause(BaseActivity act) {
55  
56      }
57  
58    }
59    
60    private HashSet<WeakReference<BasicActivityLifecycleCallbacks>> mLifeCycleCallbackSet =
61        new HashSet<WeakReference<BasicActivityLifecycleCallbacks>>();
62  
63    public void addLifeCycleObserverRef(BasicActivityLifecycleCallbacks cbRef) {
64      mLifeCycleCallbackSet.add(new WeakReference<BaseActivity.BasicActivityLifecycleCallbacks>(cbRef));
65    }
66  
67    public void removeLifeCycleObserver(BasicActivityLifecycleCallbacks removeCb) {
68      synchronized (mLifeCycleCallbackSet) {
69        ArrayList<WeakReference<BasicActivityLifecycleCallbacks>> removeList =
70            new ArrayList<WeakReference<BasicActivityLifecycleCallbacks>>();
71        for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : mLifeCycleCallbackSet) {
72          BasicActivityLifecycleCallbacks cb = cbRef == null ? null : cbRef.get();
73          if (cb == removeCb) {
74            removeList.add(cbRef);
75          }
76        }
77        for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : removeList) {
78          mLifeCycleCallbackSet.remove(cbRef);
79        }
80      }
81    }
82  
83    @Override
84    protected void onResume() {
85      super.onResume();
86      for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : mLifeCycleCallbackSet) {
87        BasicActivityLifecycleCallbacks cb = cbRef == null ? null : cbRef.get();
88        if (cb != null) {
89          cb.onResume(this);
90        }
91      }
92    }
93  
94    @Override
95    protected void onPause() {
96      for (WeakReference<BasicActivityLifecycleCallbacks> cbRef : mLifeCycleCallbackSet) {
97        BasicActivityLifecycleCallbacks cb = cbRef == null ? null : cbRef.get();
98        if (cb != null) {
99          cb.onPause(this);
100       }
101     }
102     // clear release reference
103     removeLifeCycleObserver(null);
104     if (Log.LOGD)
105       Log.d(getClass().getSimpleName(), "onPause isFinishing=", isFinishing(), " ", this);
106     super.onPause();
107   }
108 }