Update "Open networks available" toggle to instead open notification channel preferences.
Bug: 36526438 Test: make ROBOTEST_FILTER=NotifyOpenNetworkPreferenceControllerTest RunSettingsRoboTests -j40 Change-Id: Idde3949856d050e62abff2c75a7b7db475a94d99
This commit is contained in:
@@ -16,75 +16,46 @@
|
||||
|
||||
package com.android.settings.wifi;
|
||||
|
||||
import android.content.ContentResolver;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.os.Handler;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.NetworkScorerAppData;
|
||||
import android.os.RemoteException;
|
||||
import android.provider.Settings;
|
||||
import android.support.v14.preference.SwitchPreference;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import android.util.Log;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.core.lifecycle.Lifecycle;
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
import com.android.settings.network.NetworkScoreManagerWrapper;
|
||||
import com.android.settings.utils.NotificationChannelHelper;
|
||||
import com.android.settings.utils.NotificationChannelHelper.NotificationChannelWrapper;
|
||||
|
||||
/**
|
||||
* {@link PreferenceController} that controls whether we should notify user when open network is
|
||||
* available.
|
||||
* {@link PreferenceController} that shows whether we should notify user when open network is
|
||||
* available. The preference links to {@link NotificationChannel} settings.
|
||||
*/
|
||||
public class NotifyOpenNetworksPreferenceController extends PreferenceController implements
|
||||
LifecycleObserver, OnResume, OnPause {
|
||||
public class NotifyOpenNetworksPreferenceController extends PreferenceController {
|
||||
|
||||
private static final String TAG = "OpenNetworks";
|
||||
private static final String KEY_NOTIFY_OPEN_NETWORKS = "notify_open_networks";
|
||||
private SettingObserver mSettingObserver;
|
||||
|
||||
public NotifyOpenNetworksPreferenceController(Context context, Lifecycle lifecycle) {
|
||||
private NetworkScoreManagerWrapper mNetworkScoreManager;
|
||||
private NotificationChannelHelper mNotificationChannelHelper;
|
||||
private PackageManager mPackageManager;
|
||||
|
||||
public NotifyOpenNetworksPreferenceController(
|
||||
Context context,
|
||||
NetworkScoreManagerWrapper networkScoreManager,
|
||||
NotificationChannelHelper notificationChannelHelper,
|
||||
PackageManager packageManager) {
|
||||
super(context);
|
||||
lifecycle.addObserver(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mSettingObserver = new SettingObserver(screen.findPreference(KEY_NOTIFY_OPEN_NETWORKS));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
if (mSettingObserver != null) {
|
||||
mSettingObserver.register(mContext.getContentResolver(), true /* register */);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
if (mSettingObserver != null) {
|
||||
mSettingObserver.register(mContext.getContentResolver(), false /* register */);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (!TextUtils.equals(preference.getKey(), KEY_NOTIFY_OPEN_NETWORKS)) {
|
||||
return false;
|
||||
}
|
||||
if (!(preference instanceof SwitchPreference)) {
|
||||
return false;
|
||||
}
|
||||
Settings.Global.putInt(mContext.getContentResolver(),
|
||||
Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
|
||||
((SwitchPreference) preference).isChecked() ? 1 : 0);
|
||||
return true;
|
||||
mNetworkScoreManager = networkScoreManager;
|
||||
mNotificationChannelHelper = notificationChannelHelper;
|
||||
mPackageManager = packageManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -93,42 +64,59 @@ public class NotifyOpenNetworksPreferenceController extends PreferenceController
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
if (!(preference instanceof SwitchPreference)) {
|
||||
return;
|
||||
}
|
||||
final SwitchPreference notifyOpenNetworks = (SwitchPreference) preference;
|
||||
notifyOpenNetworks.setChecked(Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
|
||||
notifyOpenNetworks.setEnabled(Settings.Global.getInt(mContext.getContentResolver(),
|
||||
Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1);
|
||||
public boolean isAvailable() {
|
||||
return getNotificationChannel() != null;
|
||||
}
|
||||
|
||||
class SettingObserver extends ContentObserver {
|
||||
private final Uri NETWORK_RECOMMENDATIONS_ENABLED_URI =
|
||||
Settings.Global.getUriFor(Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED);
|
||||
|
||||
private final Preference mPreference;
|
||||
|
||||
public SettingObserver(Preference preference) {
|
||||
super(new Handler());
|
||||
mPreference = preference;
|
||||
@Override
|
||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||
if (!TextUtils.equals(preference.getKey(), KEY_NOTIFY_OPEN_NETWORKS)) {
|
||||
return false;
|
||||
}
|
||||
NetworkScorerAppData scorer = mNetworkScoreManager.getActiveScorer();
|
||||
if (scorer == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void register(ContentResolver cr, boolean register) {
|
||||
if (register) {
|
||||
cr.registerContentObserver(NETWORK_RECOMMENDATIONS_ENABLED_URI, false, this);
|
||||
} else {
|
||||
cr.unregisterContentObserver(this);
|
||||
}
|
||||
}
|
||||
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
|
||||
intent.putExtra(Settings.EXTRA_CHANNEL_ID,
|
||||
scorer.getNetworkAvailableNotificationChannelId());
|
||||
intent.putExtra(Settings.EXTRA_APP_PACKAGE, scorer.getRecommendationServicePackageName());
|
||||
mContext.startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange, Uri uri) {
|
||||
super.onChange(selfChange, uri);
|
||||
if (NETWORK_RECOMMENDATIONS_ENABLED_URI.equals(uri)) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
NotificationChannelWrapper channel = getNotificationChannel();
|
||||
if (channel == null) {
|
||||
preference.setSummary(null);
|
||||
} else {
|
||||
preference.setSummary(channel.getImportance() != NotificationManager.IMPORTANCE_NONE ?
|
||||
R.string.notification_toggle_on : R.string.notification_toggle_off);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private NotificationChannelWrapper getNotificationChannel() {
|
||||
NetworkScorerAppData scorer = mNetworkScoreManager.getActiveScorer();
|
||||
if (scorer == null) {
|
||||
return null;
|
||||
}
|
||||
String packageName = scorer.getRecommendationServicePackageName();
|
||||
String channelId = scorer.getNetworkAvailableNotificationChannelId();
|
||||
if (packageName == null || channelId == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return mNotificationChannelHelper.getNotificationChannelForPackage(
|
||||
packageName,
|
||||
mPackageManager.getPackageUid(packageName, 0 /* flags */),
|
||||
channelId,
|
||||
false /* includeDeleted */ );
|
||||
} catch (RemoteException | PackageManager.NameNotFoundException e) {
|
||||
Log.d(TAG, "Failed to get notification channel.", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user