diff --git a/android/brave_java_sources.gni b/android/brave_java_sources.gni index ce5ffe08bc5c..388d2342ada5 100644 --- a/android/brave_java_sources.gni +++ b/android/brave_java_sources.gni @@ -110,6 +110,7 @@ brave_java_sources = [ "../../brave/android/java/org/chromium/chrome/browser/site_settings/BraveSiteSettingsPreferencesBase.java", "../../brave/android/java/org/chromium/chrome/browser/site_settings/DesktopModePreferences.java", "../../brave/android/java/org/chromium/chrome/browser/site_settings/PlayYTVideoInBrowserPreferences.java", + "../../brave/android/java/org/chromium/chrome/browser/sync/BraveSyncDevices.java", "../../brave/android/java/org/chromium/chrome/browser/sync/BraveSyncService.java", "../../brave/android/java/org/chromium/chrome/browser/sync/BraveSyncServiceObserver.java", "../../brave/android/java/org/chromium/chrome/browser/toolbar/BraveHomeButton.java", diff --git a/android/java/org/chromium/chrome/browser/BraveSyncWorker.java b/android/java/org/chromium/chrome/browser/BraveSyncWorker.java index f8e2fccf701a..9cd3796b4f3c 100755 --- a/android/java/org/chromium/chrome/browser/BraveSyncWorker.java +++ b/android/java/org/chromium/chrome/browser/BraveSyncWorker.java @@ -208,50 +208,6 @@ public void HandleReset() { nativeHandleReset(mNativeBraveSyncWorker); } - @CalledByNative - public void OnDeviceInfoChangeJava() { -Log.e(TAG, "[BraveSync] HandleReset 000"); - if (mSyncScreensObserver != null) { - mSyncScreensObserver.onDevicesAvailable(); - } - } - - public class SyncDeviceInfo { - public String mName; - public boolean mIsCurrentDevice; - public String mType; - public Date mLastUpdatedTimestamp; - } - - public ArrayList GetSyncDeviceList() { -Log.e(TAG, "[BraveSync] GetSyncDeviceList 000"); - ArrayList deviceList = new ArrayList(); - String json = nativeGetSyncDeviceListJson(mNativeBraveSyncWorker); - json = "{\"devices\":"+json+"}"; -Log.e(TAG, "[BraveSync] GetSyncDeviceList json="+json); - try { - JSONObject result = new JSONObject(json); - JSONArray devices = result.getJSONArray("devices"); -Log.e(TAG, "[BraveSync] GetSyncDeviceList devices.length()="+devices.length()); - for (int i = 0; i < devices.length(); i++) { - SyncDeviceInfo deviceInfo = new SyncDeviceInfo(); - JSONObject device = devices.getJSONObject(i); - deviceInfo.mName = device.getString("name"); - deviceInfo.mIsCurrentDevice = device.getBoolean("isCurrentDevice"); - deviceInfo.mType = device.getString("type"); - long lastUpdatedTimestamp = device.getLong("lastUpdatedTimestamp"); - deviceInfo.mLastUpdatedTimestamp = new Date(lastUpdatedTimestamp); - deviceList.add(deviceInfo); - } - } catch (JSONException e) { - Log.e(TAG, "GetDeviceNameByObjectId JSONException error " + e); - } catch (IllegalStateException e) { - Log.e(TAG, "GetDeviceNameByObjectId IllegalStateException error " + e); - } -Log.e(TAG, "[BraveSync] GetSyncDeviceList deviceList.size()="+deviceList.size()); - return deviceList; - } - private native void nativeInit(); private native void nativeDestroy(long nativeBraveSyncWorker); @@ -269,6 +225,4 @@ public ArrayList GetSyncDeviceList() { private native boolean nativeIsFirstSetupComplete(long nativeBraveSyncWorker); private native void nativeHandleReset(long nativeBraveSyncWorker); - - private native String nativeGetSyncDeviceListJson(long nativeBraveSyncWorker); } diff --git a/android/java/org/chromium/chrome/browser/settings/BraveSyncScreensPreference.java b/android/java/org/chromium/chrome/browser/settings/BraveSyncScreensPreference.java index 9cbde239f7c5..8c77df1d1a33 100644 --- a/android/java/org/chromium/chrome/browser/settings/BraveSyncScreensPreference.java +++ b/android/java/org/chromium/chrome/browser/settings/BraveSyncScreensPreference.java @@ -88,6 +88,7 @@ import org.chromium.chrome.browser.sync.BraveSyncService; import org.chromium.ui.KeyboardVisibilityDelegate; import org.chromium.ui.base.DeviceFormFactor; +import org.chromium.chrome.browser.sync.BraveSyncDevices; import java.io.IOException; import java.lang.Runnable; @@ -104,7 +105,8 @@ public class BraveSyncScreensPreference extends BravePreferenceFragment implements View.OnClickListener, SettingsActivity.OnBackPressedListener, CompoundButton.OnCheckedChangeListener, BarcodeTracker.BarcodeGraphicTrackerCallback, - BraveSyncService.GetSettingsAndDevicesCallback { + BraveSyncService.GetSettingsAndDevicesCallback, + BraveSyncDevices.DeviceInfoChangedListener { private static final String TAG = "SYNC"; // Permission request codes need to be < 256 @@ -178,6 +180,19 @@ public class BraveSyncScreensPreference extends BravePreferenceFragment private FrameLayout mLayoutMobile; private FrameLayout mLayoutLaptop; + @Override + public void deviceInfoChanged() { + Log.e(TAG, "[BraveSync] BraveSyncScreensPreference.deviceInfoChanged 000"); + if (mSyncScreensObserver != null) { +Log.e(TAG, "[BraveSync] BraveSyncScreensPreference.deviceInfoChanged will call mSyncScreensObserver.onDevicesAvailable()"); + mSyncScreensObserver.onDevicesAvailable(); + } else { +Log.e(TAG, "[BraveSync] BraveSyncScreensPreference.deviceInfoChanged mSyncScreensObserver is null"); + } + } + boolean deviceInfoObserverSet = false; + + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); @@ -441,14 +456,12 @@ public void run() { Log.w(TAG, "No need to load devices for other pages"); return; } - BraveActivity mainActivity = BraveRewardsHelper.getBraveActivity(); - // if (null != mainActivity && null != mainActivity.mBraveSyncWorker) { - ArrayList deviceInfos = mainActivity.mBraveSyncWorker.GetSyncDeviceList(); + ArrayList deviceInfos = BraveSyncDevices.get().GetSyncDeviceList(); Log.e(TAG, "[BraveSync] onDevicesAvailable deviceInfos.size()="+deviceInfos.size()); ViewGroup insertPoint = (ViewGroup) getView().findViewById(R.id.brave_sync_devices); insertPoint.removeAllViews(); int index = 0; - for (BraveSyncWorker.SyncDeviceInfo device : deviceInfos) { + for (BraveSyncDevices.SyncDeviceInfo device : deviceInfos) { View separator = (View) mInflater.inflate(R.layout.menu_separator, null); View listItemView = (View) mInflater.inflate(R.layout.brave_sync_device, null); if (null != listItemView && null != separator && null != insertPoint) { @@ -1242,6 +1255,11 @@ public void onDestroy() { if (mCameraSourcePreview != null) { mCameraSourcePreview.release(); } +Log.e(TAG, "[BraveSync] BraveSyncScreensPreference.onDestroy deviceInfoObserverSet="+deviceInfoObserverSet); + if (deviceInfoObserverSet) { + BraveSyncDevices.get().removeDeviceInfoChangedListener(this); + deviceInfoObserverSet = false; + } } private boolean isBarCodeValid(String barcode, boolean hexValue) { @@ -1719,6 +1737,12 @@ private void setSyncDoneLayout() { mainActivity.mBraveSyncWorker.SaveCodephrase(GetCodephrase()); mainActivity.mBraveSyncWorker.OnDidClosePage(); +Log.e(TAG, "[BraveSync] BraveSyncScreensPreference.setSyncDoneLayout deviceInfoObserverSet="+deviceInfoObserverSet); + if (!deviceInfoObserverSet) { + BraveSyncDevices.get().addDeviceInfoChangedListener(this); + deviceInfoObserverSet = true; + } + getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); getActivity().setTitle(R.string.sync_category_title); if (null != mCameraSourcePreview) { diff --git a/browser/BUILD.gn b/browser/BUILD.gn index 36681a44f82a..83468d52a626 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -235,6 +235,13 @@ source_set("browser_process") { ] } + if (enable_brave_sync) { + sources += [ + "sync/brave_sync_devices_android.cc", + "sync/brave_sync_devices_android.h", + ] + } + deps += [ "//brave/browser/android/preferences", "//brave/build/android:jni_headers", diff --git a/browser/android/brave_sync_worker.cc b/browser/android/brave_sync_worker.cc index 4894e0db8dd4..151be464b373 100644 --- a/browser/android/brave_sync_worker.cc +++ b/browser/android/brave_sync_worker.cc @@ -19,14 +19,10 @@ #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_manager.h" -#include "chrome/browser/sync/device_info_sync_service_factory.h" #include "chrome/browser/sync/profile_sync_service_factory.h" #include "components/sync/driver/sync_service.h" #include "components/sync/driver/sync_user_settings.h" -#include "components/sync_device_info/device_info_sync_service.h" -#include "components/sync_device_info/device_info_tracker.h" -#include "components/sync_device_info/local_device_info_provider.h" #include "components/unified_consent/unified_consent_metrics.h" #include "third_party/leveldatabase/src/include/leveldb/db.h" @@ -128,14 +124,44 @@ DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; if (service && !sync_service_observer_.IsObserving(service)) { sync_service_observer_.Add(service); } +/* +sync state observer + +src/chrome/android/java/src/org/chromium/chrome/browser/sync/ProfileSyncService.java + +bool ProfileSyncServiceAndroid::Init() { + if (sync_service_) { + sync_service_->AddObserver(this); + return true; + +<= + +public void addSyncStateChangedListener(SyncStateChangedListener listener) { + ThreadUtils.assertOnUiThread(); + mListeners.add(listener); +} + +public void removeSyncStateChangedListener(SyncStateChangedListener listener) { + ThreadUtils.assertOnUiThread(); + mListeners.remove(listener); +} + +<= + + +ProfileSyncService.get().addSyncStateChangedListener(this); +@Override +public void syncStateChanged() { + if (mType != getSyncErrorInfoBarType()) { + onCloseButtonClicked(); + } +} + + + +*/ + - syncer::DeviceInfoTracker* tracker = - DeviceInfoSyncServiceFactory::GetForProfile(profile_) - ->GetDeviceInfoTracker(); - DCHECK(tracker); - if (tracker && !device_info_tracker_observer_.IsObserving(tracker) ) { - device_info_tracker_observer_.Add(tracker); - } // TODO, AB: call SetSyncRequested(true) and sync_service_observer_ must be set and only if sync is not enabled // The point when it is invoked when sync is enabled is above on the stack @@ -154,6 +180,26 @@ DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; DCHECK_CURRENTLY_ON(BrowserThread::UI); sync_service_->GetUserSettings()->SetSyncRequested(true); } + <= + public void requestStart() + <= + src/chrome/android/java/src/org/chromium/chrome/browser/sync/settings/SyncSettingsUtils.java + public static void enableSync(boolean enable) { + ProfileSyncService profileSyncService = ProfileSyncService.get(); + if (enable == profileSyncService.isSyncRequested()) return; + + if (enable) { + profileSyncService.requestStart(); + } else { + RecordHistogram.recordEnumeratedHistogram("Sync.StopSource", + StopSource.CHROME_SYNC_SETTINGS, StopSource.STOP_SOURCE_LIMIT); + profileSyncService.requestStop(); + } + } + or + + + */ } @@ -366,52 +412,6 @@ void BraveSyncWorker::OnStateChanged(syncer::SyncService* sync) { ProfileMetrics::LogProfileSyncInfo(ProfileMetrics::SYNC_PASSPHRASE); } -void BraveSyncWorker::OnDeviceInfoChange() { - DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; - // Notify UI page - JNIEnv* env = base::android::AttachCurrentThread(); - Java_BraveSyncWorker_OnDeviceInfoChangeJava(env, - weak_java_brave_sync_worker_.get(env)); -} - -base::Value BraveSyncWorker::GetSyncDeviceList() { -DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; - auto* device_info_service = - DeviceInfoSyncServiceFactory::GetForProfile(profile_); - syncer::DeviceInfoTracker* tracker = - device_info_service->GetDeviceInfoTracker(); - DCHECK(tracker); - const syncer::DeviceInfo* local_device_info = device_info_service - ->GetLocalDeviceInfoProvider()->GetLocalDeviceInfo(); - - base::Value device_list(base::Value::Type::LIST); - - for (const auto& device : tracker->GetAllDeviceInfo()) { - auto device_value = base::Value::FromUniquePtrValue(device->ToValue()); - bool is_current_device = local_device_info - ? local_device_info->guid() == device->guid() - : false; - device_value.SetBoolKey("isCurrentDevice", is_current_device); - device_list.Append(std::move(device_value)); - } -DLOG(ERROR) << "[BraveSync] " << __func__ << " device_list=" < - BraveSyncWorker::GetSyncDeviceListJson(JNIEnv* env, - const base::android::JavaParamRef& jcaller) { - base::Value device_list = GetSyncDeviceList(); - std::string json_string; - if (!base::JSONWriter::Write(device_list, &json_string)) { - DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; - json_string = std::string(); - } -DLOG(ERROR) << "[BraveSync] " << __func__ << " json_string=" << json_string; - return base::android::ConvertUTF8ToJavaString(env, json_string); -} - - static void JNI_BraveSyncWorker_Init( JNIEnv* env, const base::android::JavaParamRef& jcaller) { diff --git a/browser/android/brave_sync_worker.h b/browser/android/brave_sync_worker.h index ded73b41e135..c526bd011869 100644 --- a/browser/android/brave_sync_worker.h +++ b/browser/android/brave_sync_worker.h @@ -10,7 +10,6 @@ #include "base/android/jni_weak_ref.h" #include "base/scoped_observer.h" -#include "components/sync_device_info/device_info_tracker.h" #include "components/sync/driver/sync_service.h" #include "components/sync/driver/sync_service_observer.h" @@ -19,8 +18,7 @@ class Profile; namespace chrome { namespace android { -class BraveSyncWorker : public syncer::SyncServiceObserver, - public syncer::DeviceInfoTracker::Observer { +class BraveSyncWorker : public syncer::SyncServiceObserver { public: BraveSyncWorker(JNIEnv* env, //jobject obj @@ -50,21 +48,13 @@ class BraveSyncWorker : public syncer::SyncServiceObserver, void HandleReset(JNIEnv* env, const base::android::JavaParamRef& jcaller); - base::android::ScopedJavaLocalRef - GetSyncDeviceListJson(JNIEnv* env, - const base::android::JavaParamRef& jcaller); - private: syncer::SyncService* GetSyncService() const; void MarkFirstSetupComplete(); - base::Value GetSyncDeviceList(); // syncer::SyncServiceObserver implementation. void OnStateChanged(syncer::SyncService* sync) override; - // syncer::DeviceInfoTracker::Observer - void OnDeviceInfoChange() override; - JavaObjectWeakGlobalRef weak_java_brave_sync_worker_; Profile* profile_ = nullptr; @@ -73,9 +63,6 @@ class BraveSyncWorker : public syncer::SyncServiceObserver, ScopedObserver sync_service_observer_{this}; - ScopedObserver - device_info_tracker_observer_{this}; - DISALLOW_COPY_AND_ASSIGN(BraveSyncWorker); }; diff --git a/browser/sync/brave_sync_devices_android.cc b/browser/sync/brave_sync_devices_android.cc new file mode 100644 index 000000000000..4c8e4bb1aa4e --- /dev/null +++ b/browser/sync/brave_sync_devices_android.cc @@ -0,0 +1,108 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/sync/brave_sync_devices_android.h" + +#include "base/android/jni_android.h" +#include "base/android/jni_string.h" +#include "base/json/json_writer.h" + +#include "brave/build/android/jni_headers/BraveSyncDevices_jni.h" + +#include "chrome/browser/profiles/profile_manager.h" +#include "chrome/browser/sync/device_info_sync_service_factory.h" + +#include "components/sync_device_info/device_info_sync_service.h" +#include "components/sync_device_info/device_info_tracker.h" +#include "components/sync_device_info/local_device_info_provider.h" + +namespace chrome { +namespace android { + +BraveSyncDevicesAndroid::BraveSyncDevicesAndroid( + JNIEnv* env, const base::android::JavaRef& obj) : + weak_java_brave_sync_worker_(env, obj) { + + Java_BraveSyncDevices_setNativePtr(env, obj, + reinterpret_cast(this)); + + DLOG(ERROR) << "[BraveSync] " << __func__ << " CTOR done setNativePtr"; + profile_ = + ProfileManager::GetActiveUserProfile()->GetOriginalProfile(); + DLOG(ERROR) << "[BraveSync] " << __func__ << " profile_="<GetDeviceInfoTracker(); + DCHECK(tracker); + if (tracker) { + device_info_tracker_observer_.Add(tracker); + } +} + +BraveSyncDevicesAndroid::~BraveSyncDevicesAndroid() { + // Observer will be removed by ScopedObserver +} + +void BraveSyncDevicesAndroid::Destroy(JNIEnv* env, + const base::android::JavaParamRef& jcaller) { + delete this; +} + +void BraveSyncDevicesAndroid::OnDeviceInfoChange() { + DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; + // Notify Java code + JNIEnv* env = base::android::AttachCurrentThread(); + Java_BraveSyncDevices_deviceInfoChanged(env, + weak_java_brave_sync_worker_.get(env)); +} + +base::Value BraveSyncDevicesAndroid::GetSyncDeviceList() { +DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; + auto* device_info_service = + DeviceInfoSyncServiceFactory::GetForProfile(profile_); + syncer::DeviceInfoTracker* tracker = + device_info_service->GetDeviceInfoTracker(); + DCHECK(tracker); + const syncer::DeviceInfo* local_device_info = device_info_service + ->GetLocalDeviceInfoProvider()->GetLocalDeviceInfo(); + + base::Value device_list(base::Value::Type::LIST); + + for (const auto& device : tracker->GetAllDeviceInfo()) { + auto device_value = base::Value::FromUniquePtrValue(device->ToValue()); + bool is_current_device = local_device_info + ? local_device_info->guid() == device->guid() + : false; + device_value.SetBoolKey("isCurrentDevice", is_current_device); + device_list.Append(std::move(device_value)); + } +DLOG(ERROR) << "[BraveSync] " << __func__ << " device_list=" < + BraveSyncDevicesAndroid::GetSyncDeviceListJson(JNIEnv* env, + const base::android::JavaParamRef& jcaller) { + base::Value device_list = GetSyncDeviceList(); + std::string json_string; + if (!base::JSONWriter::Write(device_list, &json_string)) { + DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; + json_string = std::string(); + } +DLOG(ERROR) << "[BraveSync] " << __func__ << " json_string=" << json_string; + return base::android::ConvertUTF8ToJavaString(env, json_string); +} + +static void JNI_BraveSyncDevices_Init( + JNIEnv* env, + const base::android::JavaParamRef& jcaller) { +DLOG(ERROR) << "[BraveSync] " << __func__ << " 000"; + new BraveSyncDevicesAndroid(env, jcaller); +} + +} // namespace android +} // namespace chrome diff --git a/browser/sync/brave_sync_devices_android.h b/browser/sync/brave_sync_devices_android.h new file mode 100644 index 000000000000..c7670eeb9eb1 --- /dev/null +++ b/browser/sync/brave_sync_devices_android.h @@ -0,0 +1,51 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_SYNC_BRAVE_SYNC_DEVICES_ANDROID_H_ +#define BRAVE_BROWSER_SYNC_BRAVE_SYNC_DEVICES_ANDROID_H_ + +#include + +#include "base/android/jni_weak_ref.h" +#include "base/values.h" +#include "base/scoped_observer.h" +#include "chrome/browser/sync/profile_sync_service_android.h" +#include "components/sync_device_info/device_info_tracker.h" + +namespace chrome { +namespace android { + + +class BraveSyncDevicesAndroid : public syncer::DeviceInfoTracker::Observer { + public: + BraveSyncDevicesAndroid(JNIEnv* env, + const base::android::JavaRef& obj); + virtual ~BraveSyncDevicesAndroid(); + + void Destroy(JNIEnv* env, + const base::android::JavaParamRef& jcaller); + + base::android::ScopedJavaLocalRef + GetSyncDeviceListJson(JNIEnv* env, + const base::android::JavaParamRef& jcaller); + + private: + // syncer::DeviceInfoTracker::Observer + void OnDeviceInfoChange() override; + + base::Value GetSyncDeviceList(); + + + ScopedObserver + device_info_tracker_observer_{this}; + + JavaObjectWeakGlobalRef weak_java_brave_sync_worker_; + Profile* profile_ = nullptr; +}; + +} // namespace android +} // namespace chrome + +#endif // BRAVE_BROWSER_SYNC_BRAVE_SYNC_DEVICES_ANDROID_H_ diff --git a/build/android/BUILD.gn b/build/android/BUILD.gn index c276b0d83d18..b50987026b6f 100755 --- a/build/android/BUILD.gn +++ b/build/android/BUILD.gn @@ -155,5 +155,6 @@ generate_jni("jni_headers") { "//brave/android/java/org/chromium/chrome/browser/preferences/BravePrefServiceBridge.java", "//brave/android/java/org/chromium/chrome/browser/preferences/website/BraveShieldsContentSettings.java", "//brave/android/java/org/chromium/chrome/browser/signin/BraveSigninManager.java", + "//brave/android/java/org/chromium/chrome/browser/sync/BraveSyncDevices.java", ] }