Commit 8b97e2b5 authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

fix: update channels on locale change

Bug: 710925
Change-Id: I357c105c1aa40b0acca41edde0423141fb6f62c0
Reviewed-on: https://chromium-review.googlesource.com/c/1304793
Commit-Queue: Richard Knoll <knollr@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604887}
parent cc29727b
......@@ -350,6 +350,14 @@ by a child template that "extends" this file.
</intent-filter>
</receiver>
<!-- Locale related -->
<receiver android:name="org.chromium.chrome.browser.locale.LocaleChangedBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED" />
</intent-filter>
</receiver>
<!-- Document mode Activities. -->
<activity android:name="org.chromium.chrome.browser.document.DocumentActivity"
android:exported="false"
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.locale;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import org.chromium.base.task.AsyncTask;
import org.chromium.chrome.browser.notifications.channels.ChannelsUpdater;
/**
* Triggered when Android's locale changes.
*/
public class LocaleChangedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) return;
updateChannels();
}
/**
* Updates notification channels to reflect the new locale.
*/
private void updateChannels() {
final PendingResult result = goAsync();
new AsyncTask<Void>() {
@Override
protected Void doInBackground() {
ChannelsUpdater.getInstance().updateLocale();
result.finish();
return null;
}
}
.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
}
......@@ -18,6 +18,7 @@ import org.chromium.chrome.browser.webapps.WebApkServiceClient;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
/**
* Initializes our notification channels.
......@@ -25,7 +26,7 @@ import java.util.HashMap;
@TargetApi(Build.VERSION_CODES.O)
public class ChannelsInitializer {
private final NotificationManagerProxy mNotificationManager;
private final Resources mResources;
private Resources mResources;
public ChannelsInitializer(
NotificationManagerProxy notificationManagerProxy, Resources resources) {
......@@ -41,6 +42,20 @@ public class ChannelsInitializer {
ensureInitialized(ChannelDefinitions.getStartupChannelIds());
}
/**
* Updates all the channels to reflect the correct locale.
*
* @param resources The new resources to use.
*/
void updateLocale(Resources resources) {
mResources = resources;
HashSet<String> channelIds = new HashSet<>();
for (NotificationChannel channel : mNotificationManager.getNotificationChannels()) {
channelIds.add(channel.getId());
}
ensureInitialized(channelIds);
}
/**
* Cleans up any old channels that are no longer required from previous versions of the app.
* It's safe to call this multiple times since deleting an already-deleted channel is a no-op.
......
......@@ -65,6 +65,12 @@ public class ChannelsUpdater {
storeChannelVersionInPrefs();
}
public void updateLocale() {
if (!mIsAtLeastO) return;
assert mChannelsInitializer != null;
mChannelsInitializer.updateLocale(ContextUtils.getApplicationContext().getResources());
}
private void storeChannelVersionInPrefs() {
assert mSharedPreferences != null;
mSharedPreferences.edit().putInt(CHANNELS_VERSION_KEY, mChannelsVersion).apply();
......
......@@ -799,6 +799,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/lifecycle/StartStopWithNativeObserver.java",
"java/src/org/chromium/chrome/browser/locale/DefaultSearchEngineDialogHelper.java",
"java/src/org/chromium/chrome/browser/locale/DefaultSearchEnginePromoDialog.java",
"java/src/org/chromium/chrome/browser/locale/LocaleChangedBroadcastReceiver.java",
"java/src/org/chromium/chrome/browser/locale/LocaleManager.java",
"java/src/org/chromium/chrome/browser/locale/SogouPromoDialog.java",
"java/src/org/chromium/chrome/browser/locale/LocaleTemplateUrlLoader.java",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment