1 /*
2  * Copyright (C) 2009 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.camera;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 
22 import java.util.ArrayList;
23 
24 public class MonitoredActivity extends NoSearchActivity {
25 
26     private final ArrayList<LifeCycleListener> mListeners =
27             new ArrayList<LifeCycleListener>();
28 
29     public static interface LifeCycleListener {
onActivityCreated(MonitoredActivity activity)30         public void onActivityCreated(MonitoredActivity activity);
onActivityDestroyed(MonitoredActivity activity)31         public void onActivityDestroyed(MonitoredActivity activity);
onActivityStarted(MonitoredActivity activity)32         public void onActivityStarted(MonitoredActivity activity);
onActivityStopped(MonitoredActivity activity)33         public void onActivityStopped(MonitoredActivity activity);
34     }
35 
36     public static class LifeCycleAdapter implements LifeCycleListener {
onActivityCreated(MonitoredActivity activity)37         public void onActivityCreated(MonitoredActivity activity) {
38         }
39 
onActivityDestroyed(MonitoredActivity activity)40         public void onActivityDestroyed(MonitoredActivity activity) {
41         }
42 
onActivityStarted(MonitoredActivity activity)43         public void onActivityStarted(MonitoredActivity activity) {
44         }
45 
onActivityStopped(MonitoredActivity activity)46         public void onActivityStopped(MonitoredActivity activity) {
47         }
48     }
49 
addLifeCycleListener(LifeCycleListener listener)50     public void addLifeCycleListener(LifeCycleListener listener) {
51         if (mListeners.contains(listener)) return;
52         mListeners.add(listener);
53     }
54 
removeLifeCycleListener(LifeCycleListener listener)55     public void removeLifeCycleListener(LifeCycleListener listener) {
56         mListeners.remove(listener);
57     }
58 
59     @Override
onCreate(Bundle savedInstanceState)60     protected void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         for (LifeCycleListener listener : mListeners) {
63             listener.onActivityCreated(this);
64         }
65     }
66 
67     @Override
onDestroy()68     protected void onDestroy() {
69         super.onDestroy();
70         for (LifeCycleListener listener : mListeners) {
71             listener.onActivityDestroyed(this);
72         }
73     }
74 
75     @Override
onStart()76     protected void onStart() {
77         super.onStart();
78         for (LifeCycleListener listener : mListeners) {
79             listener.onActivityStarted(this);
80         }
81     }
82 
83     @Override
onStop()84     protected void onStop() {
85         super.onStop();
86         for (LifeCycleListener listener : mListeners) {
87             listener.onActivityStopped(this);
88         }
89     }
90 }
91