Add NetworkConnectivityUtils class

Change-Id: I7bd71f1d4ef0fc0fe21ddfd9f3e11463f53ac4e7
main
Tadashi G. Takaoka 2014-11-07 13:42:13 -08:00
parent 370f129b58
commit 2b25f674f2
4 changed files with 123 additions and 35 deletions

View File

@ -40,11 +40,13 @@ import com.android.inputmethod.latin.define.ProductionFlags;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.settings.SettingsValues; import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.utils.CapsModeUtils; import com.android.inputmethod.latin.utils.CapsModeUtils;
import com.android.inputmethod.latin.utils.NetworkConnectivityUtils;
import com.android.inputmethod.latin.utils.RecapitalizeStatus; import com.android.inputmethod.latin.utils.RecapitalizeStatus;
import com.android.inputmethod.latin.utils.ResourceUtils; import com.android.inputmethod.latin.utils.ResourceUtils;
import com.android.inputmethod.latin.utils.ScriptUtils; import com.android.inputmethod.latin.utils.ScriptUtils;
public final class KeyboardSwitcher implements KeyboardState.SwitchActions { public final class KeyboardSwitcher implements KeyboardState.SwitchActions,
NetworkConnectivityUtils.NetworkStateChangeListener {
private static final String TAG = KeyboardSwitcher.class.getSimpleName(); private static final String TAG = KeyboardSwitcher.class.getSimpleName();
private SubtypeSwitcher mSubtypeSwitcher; private SubtypeSwitcher mSubtypeSwitcher;
@ -414,6 +416,8 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
return mCurrentInputView; return mCurrentInputView;
} }
// {@link NetworkConnectivityUtils.NetworkStateChangeListener#onNetworkStateChanged(boolean)}.
@Override
public void onNetworkStateChanged() { public void onNetworkStateChanged() {
if (mKeyboardView == null) { if (mKeyboardView == null) {
return; return;

View File

@ -32,7 +32,6 @@ import android.content.res.Configuration;
import android.content.res.Resources; import android.content.res.Resources;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.os.Build; import android.os.Build;
import android.os.Debug; import android.os.Debug;
import android.os.IBinder; import android.os.IBinder;
@ -100,6 +99,7 @@ import com.android.inputmethod.latin.utils.ImportantNoticeUtils;
import com.android.inputmethod.latin.utils.IntentUtils; import com.android.inputmethod.latin.utils.IntentUtils;
import com.android.inputmethod.latin.utils.JniUtils; import com.android.inputmethod.latin.utils.JniUtils;
import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper;
import com.android.inputmethod.latin.utils.NetworkConnectivityUtils;
import com.android.inputmethod.latin.utils.StatsUtils; import com.android.inputmethod.latin.utils.StatsUtils;
import com.android.inputmethod.latin.utils.StatsUtilsManager; import com.android.inputmethod.latin.utils.StatsUtilsManager;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
@ -592,13 +592,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
loadSettings(); loadSettings();
resetDictionaryFacilitatorIfNecessary(); resetDictionaryFacilitatorIfNecessary();
// Register to receive ringer mode change and network state change. NetworkConnectivityUtils.onCreate(this /* context */, mKeyboardSwitcher /* listener */);
// Also receive installation and removal of a dictionary pack.
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(mConnectivityAndRingerModeChangeReceiver, filter);
// Register to receive ringer mode change.
final IntentFilter filter = new IntentFilter();
filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(mRingerModeChangeReceiver, filter);
// Register to receive installation and removal of a dictionary pack.
final IntentFilter packageFilter = new IntentFilter(); final IntentFilter packageFilter = new IntentFilter();
packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED); packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
@ -726,7 +727,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mPersonalizationDictionaryUpdater.onDestroy(); mPersonalizationDictionaryUpdater.onDestroy();
mContextualDictionaryUpdater.onDestroy(); mContextualDictionaryUpdater.onDestroy();
mSettings.onDestroy(); mSettings.onDestroy();
unregisterReceiver(mConnectivityAndRingerModeChangeReceiver); NetworkConnectivityUtils.onDestroy(this /* context */);
unregisterReceiver(mRingerModeChangeReceiver);
unregisterReceiver(mDictionaryPackInstallReceiver); unregisterReceiver(mDictionaryPackInstallReceiver);
unregisterReceiver(mDictionaryDumpBroadcastReceiver); unregisterReceiver(mDictionaryDumpBroadcastReceiver);
mStatsUtilsManager.onDestroy(); mStatsUtilsManager.onDestroy();
@ -738,7 +740,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
public void recycle() { public void recycle() {
unregisterReceiver(mDictionaryPackInstallReceiver); unregisterReceiver(mDictionaryPackInstallReceiver);
unregisterReceiver(mDictionaryDumpBroadcastReceiver); unregisterReceiver(mDictionaryDumpBroadcastReceiver);
unregisterReceiver(mConnectivityAndRingerModeChangeReceiver); unregisterReceiver(mRingerModeChangeReceiver);
NetworkConnectivityUtils.onDestroy(this /* context */);
mInputLogic.recycle(); mInputLogic.recycle();
} }
@ -1828,15 +1831,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// boolean onKeyLongPress(final int keyCode, final KeyEvent event); // boolean onKeyLongPress(final int keyCode, final KeyEvent event);
// boolean onKeyMultiple(final int keyCode, final int count, final KeyEvent event); // boolean onKeyMultiple(final int keyCode, final int count, final KeyEvent event);
// receive ringer mode change and network state change. // receive ringer mode change.
private final BroadcastReceiver mConnectivityAndRingerModeChangeReceiver = private final BroadcastReceiver mRingerModeChangeReceiver = new BroadcastReceiver() {
new BroadcastReceiver() {
@Override @Override
public void onReceive(final Context context, final Intent intent) { public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction(); final String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
mRichImm.onNetworkStateChanged(intent);
} else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
AudioAndHapticFeedbackManager.getInstance().onRingerModeChanged(); AudioAndHapticFeedbackManager.getInstance().onRingerModeChanged();
} }
} }

View File

@ -20,11 +20,8 @@ import static com.android.inputmethod.latin.common.Constants.Subtype.KEYBOARD_MO
import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.REQ_NETWORK_CONNECTIVITY; import static com.android.inputmethod.latin.common.Constants.Subtype.ExtraValue.REQ_NETWORK_CONNECTIVITY;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
@ -36,10 +33,10 @@ import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.compat.InputMethodManagerCompatWrapper; import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
import com.android.inputmethod.keyboard.KeyboardSwitcher;
import com.android.inputmethod.latin.settings.AdditionalFeaturesSettingUtils; import com.android.inputmethod.latin.settings.AdditionalFeaturesSettingUtils;
import com.android.inputmethod.latin.settings.Settings; import com.android.inputmethod.latin.settings.Settings;
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
import com.android.inputmethod.latin.utils.NetworkConnectivityUtils;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
import java.util.Collections; import java.util.Collections;
@ -72,7 +69,6 @@ public class RichInputMethodManager {
private RichInputMethodSubtype mCurrentRichInputMethodSubtype; private RichInputMethodSubtype mCurrentRichInputMethodSubtype;
private InputMethodInfo mShortcutInputMethodInfo; private InputMethodInfo mShortcutInputMethodInfo;
private InputMethodSubtype mShortcutSubtype; private InputMethodSubtype mShortcutSubtype;
private boolean mIsNetworkConnected;
final HashMap<InputMethodInfo, List<InputMethodSubtype>> final HashMap<InputMethodInfo, List<InputMethodSubtype>>
mSubtypeListCacheWithImplicitlySelectedSubtypes = new HashMap<>(); mSubtypeListCacheWithImplicitlySelectedSubtypes = new HashMap<>();
final HashMap<InputMethodInfo, List<InputMethodSubtype>> final HashMap<InputMethodInfo, List<InputMethodSubtype>>
@ -116,11 +112,6 @@ public class RichInputMethodManager {
// Initialize the current input method subtype and the shortcut IME. // Initialize the current input method subtype and the shortcut IME.
refreshSubtypeCaches(); refreshSubtypeCaches();
final ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
mIsNetworkConnected = (info != null && info.isConnected());
} }
public InputMethodSubtype[] getAdditionalSubtypes() { public InputMethodSubtype[] getAdditionalSubtypes() {
@ -591,16 +582,8 @@ public class RichInputMethodManager {
return true; return true;
} }
if (mShortcutSubtype.containsExtraValueKey(REQ_NETWORK_CONNECTIVITY)) { if (mShortcutSubtype.containsExtraValueKey(REQ_NETWORK_CONNECTIVITY)) {
return mIsNetworkConnected; return NetworkConnectivityUtils.isNetworkConnected();
} }
return true; return true;
} }
public void onNetworkStateChanged(final Intent intent) {
final boolean noConnection = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
mIsNetworkConnected = !noConnection;
KeyboardSwitcher.getInstance().onNetworkStateChanged();
}
} }

View File

@ -0,0 +1,101 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.inputmethod.latin.utils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import javax.annotation.Nonnull;
/**
* This class keeps track of the network connectivity state by receiving the system intent
* {@link ConnectivityManager#CONNECTIVITY_ACTION}, and invokes an registered call back to notify
* changes of the network connectivity state.
*/
public final class NetworkConnectivityUtils {
private static NetworkConnectivityReceiver sNetworkConnectivityReceiver;
public interface NetworkStateChangeListener {
/**
* Called when the network connectivity state has changed.
*/
public void onNetworkStateChanged();
}
private static class NetworkConnectivityReceiver extends BroadcastReceiver {
@Nonnull
private final NetworkStateChangeListener mListener;
private boolean mIsNetworkConnected;
public NetworkConnectivityReceiver(@Nonnull final NetworkStateChangeListener listener,
final boolean isNetworkConnected) {
mListener = listener;
mIsNetworkConnected = isNetworkConnected;
}
public synchronized boolean isNetworkConnected() {
return mIsNetworkConnected;
}
@Override
public void onReceive(final Context context, final Intent intent) {
final String action = intent.getAction();
if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
final boolean noConnection = intent.getBooleanExtra(
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
synchronized (this) {
mIsNetworkConnected = !noConnection;
}
mListener.onNetworkStateChanged();
}
}
}
private NetworkConnectivityUtils() {
// This utility class is not publicly instantiable.
}
public static void onCreate(@Nonnull final Context context,
@Nonnull final NetworkStateChangeListener listener) {
final ConnectivityManager connectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo info = connectivityManager.getActiveNetworkInfo();
final boolean isNetworkConnected = (info != null && info.isConnected());
// Register {@link BroadcastReceiver} for the network connectivity state change.
final NetworkConnectivityReceiver receiver = new NetworkConnectivityReceiver(
listener, isNetworkConnected);
final IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
context.registerReceiver(receiver, filter);
sNetworkConnectivityReceiver = receiver;
}
public static void onDestroy(final Context context) {
context.unregisterReceiver(sNetworkConnectivityReceiver);
}
public static boolean isNetworkConnected() {
final NetworkConnectivityReceiver receiver = sNetworkConnectivityReceiver;
return receiver != null && receiver.isNetworkConnected();
}
}