EmailConnectivityManager.java revision 3ef8f54bae6a3e02919cfd7add7ed6bf7fdda901
1/*
2 * Copyright (C) 2011 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.email;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.net.ConnectivityManager;
24import android.net.NetworkInfo;
25import android.net.NetworkInfo.State;
26import android.os.Bundle;
27import android.os.PowerManager;
28import android.os.PowerManager.WakeLock;
29import android.util.Log;
30
31/**
32 * Encapsulates functionality of ConnectivityManager for use in the Email application.  In
33 * particular, this class provides callbacks for connectivity lost, connectivity restored, and
34 * background setting changed, as well as providing a method that waits for connectivity
35 * to be available without holding a wake lock
36 *
37 * To use, EmailConnectivityManager mgr = new EmailConnectivityManager(context, "Name");
38 * When done, mgr.unregister() to unregister the internal receiver
39 *
40 * TODO: Use this class in ExchangeService
41 */
42public class EmailConnectivityManager extends BroadcastReceiver {
43    private static final String TAG = "EmailConnectivityManager";
44
45    // Loop time while waiting (stopgap in case we don't get a broadcast)
46    private static final int CONNECTIVITY_WAIT_TIME = 10*60*1000;
47
48    // The name of this manager (used for logging)
49    private final String mName;
50    // The monitor lock we use while waiting for connectivity
51    private final Object mLock = new Object();
52    // The instantiator's context
53    private final Context mContext;
54    // The wake lock used while running (so we don't fall asleep during execution/callbacks)
55    private final WakeLock mWakeLock;
56    private final android.net.ConnectivityManager mConnectivityManager;
57
58    // Set when we abort waitForConnectivity() via stopWait
59    private boolean mStop = false;
60    // The thread waiting for connectivity
61    private Thread mWaitThread;
62    // Whether or not we're registered with the system connectivity manager
63    private boolean mRegistered = true;
64
65    public EmailConnectivityManager(Context context, String name)  {
66        mContext = context;
67        mName = name;
68        mConnectivityManager =
69            (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
70        PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
71        mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
72        mContext.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
73    }
74
75    public boolean isBackgroundDataAllowed() {
76        return mConnectivityManager.getBackgroundDataSetting();
77    }
78
79    public void stopWait() {
80        mStop = true;
81        Thread thread= mWaitThread;
82        if (thread != null) {
83            thread.interrupt();
84        }
85    }
86
87    /**
88     * Called when network connectivity has been restored; this method should be overridden by
89     * subclasses as necessary. NOTE: CALLED ON UI THREAD
90     * @param networkType as defined by ConnectivityManager
91     */
92    public void onConnectivityRestored(int networkType) {
93    }
94
95    /**
96     * Called when network connectivity has been lost; this method should be overridden by
97     * subclasses as necessary. NOTE: CALLED ON UI THREAD
98     * @param networkType as defined by ConnectivityManager
99     */
100    public void onConnectivityLost(int networkType) {
101    }
102
103    /**
104     * Called when the user changes the state of the "Background Data" setting; this method should
105     * be overridden by subclasses as necessary.  NOTE: CALLED ON UI THREAD
106     * @param state the new state of the "Background Data" setting
107     */
108    public void onBackgroundDataChanged(boolean state) {
109    }
110
111    public void unregister() {
112        try {
113            mContext.unregisterReceiver(this);
114        } catch (RuntimeException e) {
115            // Don't crash if we didn't register
116        } finally {
117            mRegistered = false;
118        }
119    }
120
121    @Override
122    public void onReceive(Context context, Intent intent) {
123        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
124            Bundle extras = intent.getExtras();
125            if (extras != null) {
126                NetworkInfo networkInfo =
127                    (NetworkInfo)extras.get(ConnectivityManager.EXTRA_NETWORK_INFO);
128                if (networkInfo == null) return;
129                State state = networkInfo.getState();
130                if (state == State.CONNECTED) {
131                    synchronized (mLock) {
132                        mLock.notifyAll();
133                    }
134                    onConnectivityRestored(networkInfo.getType());
135                } else if (state == State.DISCONNECTED) {
136                    onConnectivityLost(networkInfo.getType());
137                }
138            }
139        } else if (intent.getAction().equals(
140                ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED)) {
141            onBackgroundDataChanged(isBackgroundDataAllowed());
142        }
143    }
144
145    public void waitForConnectivity() {
146        // If we're unregistered, throw an exception
147        if (!mRegistered) {
148            throw new IllegalStateException("ConnectivityManager not registered");
149        }
150        boolean waiting = false;
151        mWaitThread = Thread.currentThread();
152        // Acquire the wait lock while we work
153        mWakeLock.acquire();
154        try {
155            while (!mStop) {
156                NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
157                if (info != null) {
158                    // We're done if there's an active network
159                    if (waiting) {
160                        if (Email.DEBUG) {
161                            Log.d(TAG, mName + ": Connectivity wait ended");
162                        }
163                    }
164                    return;
165                } else {
166                    if (!waiting) {
167                        if (Email.DEBUG) {
168                            Log.d(TAG, mName + ": Connectivity waiting...");
169                        }
170                        waiting = true;
171                    }
172                    // Wait until a network is connected (or 10 mins), but let the device sleep
173                    synchronized (mLock) {
174                        // Don't hold a lock during our wait
175                        mWakeLock.release();
176                        try {
177                            mLock.wait(CONNECTIVITY_WAIT_TIME);
178                        } catch (InterruptedException e) {
179                            // This is fine; we just go around the loop again
180                        }
181                        // Get the lock back and check again for connectivity
182                        mWakeLock.acquire();
183                    }
184                }
185            }
186        } finally {
187            // Make sure we always release the wait lock
188            if (mWakeLock.isHeld()) {
189                mWakeLock.release();
190            }
191            mWaitThread = null;
192        }
193    }
194}
195