[go: up one dir, main page]

[Dark] Fix send tab to self dark mode

Use context of triggering activity to inflate views instead of
application context so that dark mode works properly.

(cherry picked from commit 9b62405e30e8ab0008e8baa2a4a6a794fd5d86c8)

Bug: 972834
Change-Id: I16614056980105726b451f39b3c632fccc75f711
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1652652
Reviewed-by: Tanya Gupta <tgupta@chromium.org>
Reviewed-by: Theresa <twellington@chromium.org>
Commit-Queue: Becky Zhou <huayinz@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#668174}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1656789
Reviewed-by: Becky Zhou <huayinz@chromium.org>
Cr-Commit-Position: refs/branch-heads/3809@{#268}
Cr-Branched-From: d82dec1a818f378c464ba307ddd9c92133eac355-refs/heads/master@{#665002}
diff --git a/chrome/android/java/res/layout/send_tab_to_self_device_picker_toolbar.xml b/chrome/android/java/res/layout/send_tab_to_self_device_picker_toolbar.xml
index 574ea3e..b0f41da 100644
--- a/chrome/android/java/res/layout/send_tab_to_self_device_picker_toolbar.xml
+++ b/chrome/android/java/res/layout/send_tab_to_self_device_picker_toolbar.xml
@@ -8,8 +8,7 @@
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
-    android:gravity="center_vertical"
-    android:background="@android:color/white">
+    android:gravity="center_vertical">
         <TextView
             android:id="@+id/device_picker_toolbar"
             android:layout_width="match_parent"
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetAdapter.java
index 85fff96..3649635 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetAdapter.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetAdapter.java
@@ -5,6 +5,7 @@
 package org.chromium.chrome.browser.send_tab_to_self;
 
 import android.content.Context;
+import android.content.res.Resources;
 import android.graphics.drawable.Drawable;
 import android.support.v7.content.res.AppCompatResources;
 import android.view.LayoutInflater;
@@ -13,7 +14,6 @@
 import android.widget.BaseAdapter;
 import android.widget.TextView;
 
-import org.chromium.base.ContextUtils;
 import org.chromium.chrome.R;
 import org.chromium.chrome.browser.profiles.Profile;
 import org.chromium.ui.widget.ChromeImageView;
@@ -26,13 +26,9 @@
  * Adapter to populate the Target Device Picker sheet.
  */
 public class DevicePickerBottomSheetAdapter extends BaseAdapter {
-    private final LayoutInflater mInflator;
-    private final Context mContext;
     private final List<TargetDeviceInfo> mTargetDevices;
 
     public DevicePickerBottomSheetAdapter(Profile profile) {
-        mContext = ContextUtils.getApplicationContext();
-        mInflator = LayoutInflater.from(mContext);
         mTargetDevices = SendTabToSelfAndroidBridge.getAllTargetDeviceInfos(profile);
     }
 
@@ -54,12 +50,13 @@
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         if (convertView == null) {
-            convertView =
-                    mInflator.inflate(R.layout.send_tab_to_self_device_picker_item, parent, false);
+            final Context context = parent.getContext();
+            convertView = LayoutInflater.from(context).inflate(
+                    R.layout.send_tab_to_self_device_picker_item, parent, false);
 
             TargetDeviceInfo deviceInfo = getItem(position);
             ChromeImageView deviceIcon = convertView.findViewById(R.id.device_icon);
-            deviceIcon.setImageDrawable(getDrawableForDeviceType(deviceInfo));
+            deviceIcon.setImageDrawable(getDrawableForDeviceType(context, deviceInfo));
             deviceIcon.setVisibility(View.VISIBLE);
 
             TextView deviceName = convertView.findViewById(R.id.device_name);
@@ -69,36 +66,35 @@
 
             long numDaysDeviceActive = TimeUnit.MILLISECONDS.toDays(
                     Calendar.getInstance().getTimeInMillis() - deviceInfo.lastUpdatedTimestamp);
-            lastActive.setText(getLastActiveMessage(numDaysDeviceActive));
+            lastActive.setText(getLastActiveMessage(context.getResources(), numDaysDeviceActive));
         }
         return convertView;
     }
 
-    private String getLastActiveMessage(long numDays) {
+    private static String getLastActiveMessage(Resources resources, long numDays) {
         if (numDays < 1) {
-            return mContext.getResources().getString(
-                    R.string.send_tab_to_self_device_last_active_today);
+            return resources.getString(R.string.send_tab_to_self_device_last_active_today);
         } else if (numDays == 1) {
-            return mContext.getResources().getString(
-                    R.string.send_tab_to_self_device_last_active_one_day_ago);
+            return resources.getString(R.string.send_tab_to_self_device_last_active_one_day_ago);
         } else {
-            return mContext.getResources().getString(
+            return resources.getString(
                     R.string.send_tab_to_self_device_last_active_more_than_one_day, numDays);
         }
     }
 
-    private Drawable getDrawableForDeviceType(TargetDeviceInfo targetDevice) {
+    private static Drawable getDrawableForDeviceType(
+            Context context, TargetDeviceInfo targetDevice) {
         switch (targetDevice.deviceType) {
             case TargetDeviceInfo.DeviceType.CHROMEOS:
             case TargetDeviceInfo.DeviceType.LINUX:
             case TargetDeviceInfo.DeviceType.MACOSX:
             case TargetDeviceInfo.DeviceType.WIN: {
-                return AppCompatResources.getDrawable(mContext, R.drawable.computer_black_24dp);
+                return AppCompatResources.getDrawable(context, R.drawable.computer_black_24dp);
             }
             case TargetDeviceInfo.DeviceType.PHONE: {
-                return AppCompatResources.getDrawable(mContext, R.drawable.smartphone_black_24dp);
+                return AppCompatResources.getDrawable(context, R.drawable.smartphone_black_24dp);
             }
         }
-        return AppCompatResources.getDrawable(mContext, R.drawable.devices_black_24dp);
+        return AppCompatResources.getDrawable(context, R.drawable.devices_black_24dp);
     }
 }
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetContent.java b/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetContent.java
index 8c2185a2..9829c66 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetContent.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/DevicePickerBottomSheetContent.java
@@ -4,7 +4,6 @@
 
 package org.chromium.chrome.browser.send_tab_to_self;
 
-import android.content.Context;
 import android.content.res.Resources;
 import android.view.LayoutInflater;
 import android.view.View;
@@ -29,16 +28,13 @@
  * chosen to share it with themselves through the SendTabToSelfFeature.
  */
 public class DevicePickerBottomSheetContent implements BottomSheetContent, OnItemClickListener {
-    Context mContext;
-    ChromeActivity mActivity;
-    ViewGroup mToolbarView;
-    ViewGroup mContentView;
-    DevicePickerBottomSheetAdapter mAdapter;
-    NavigationEntry mEntry;
+    private ChromeActivity mActivity;
+    private ViewGroup mToolbarView;
+    private ViewGroup mContentView;
+    private DevicePickerBottomSheetAdapter mAdapter;
+    private NavigationEntry mEntry;
 
-    public DevicePickerBottomSheetContent(
-            Context context, ChromeActivity activity, NavigationEntry entry) {
-        mContext = context;
+    public DevicePickerBottomSheetContent(ChromeActivity activity, NavigationEntry entry) {
         mActivity = activity;
         mAdapter = new DevicePickerBottomSheetAdapter(
                 activity.getActivityTabProvider().get().getProfile());
@@ -56,14 +52,14 @@
     }
 
     private void createToolbarView() {
-        mToolbarView = (ViewGroup) LayoutInflater.from(mContext).inflate(
+        mToolbarView = (ViewGroup) LayoutInflater.from(mActivity).inflate(
                 R.layout.send_tab_to_self_device_picker_toolbar, null);
         TextView toolbarText = mToolbarView.findViewById(R.id.device_picker_toolbar);
         toolbarText.setText(R.string.send_tab_to_self_sheet_toolbar);
     }
 
     private void createContentView() {
-        mContentView = (ViewGroup) LayoutInflater.from(mContext).inflate(
+        mContentView = (ViewGroup) LayoutInflater.from(mActivity).inflate(
                 R.layout.send_tab_to_self_device_picker_list, null);
         ListView listView = mContentView.findViewById(R.id.device_picker_list);
 
@@ -144,7 +140,7 @@
         SendTabToSelfAndroidBridge.addEntry(tab.getProfile(), mEntry.getUrl(), mEntry.getTitle(),
                 mEntry.getTimestamp(), targetDeviceInfo.cacheGuid);
 
-        Resources res = mContext.getResources();
+        Resources res = mActivity.getResources();
         String toastMessage =
                 res.getString(R.string.send_tab_to_self_toast, targetDeviceInfo.deviceName);
         Toast.makeText(mActivity, toastMessage, Toast.LENGTH_SHORT).show();
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/SendTabToSelfShareActivity.java b/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/SendTabToSelfShareActivity.java
index 5958b59a..e42c7d0 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/SendTabToSelfShareActivity.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/send_tab_to_self/SendTabToSelfShareActivity.java
@@ -37,7 +37,7 @@
 
     @VisibleForTesting
     BottomSheetContent createBottomSheetContent(ChromeActivity activity, NavigationEntry entry) {
-        return new DevicePickerBottomSheetContent(getApplicationContext(), activity, entry);
+        return new DevicePickerBottomSheetContent(activity, entry);
     }
 
     public static boolean featureIsAvailable(Tab currentTab) {