Skip to content

Commit

Permalink
Moved work with devices to separate Java class
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Jun 14, 2020
1 parent e80f429 commit 4088d45
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 122 deletions.
1 change: 1 addition & 0 deletions android/brave_java_sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
46 changes: 0 additions & 46 deletions android/java/org/chromium/chrome/browser/BraveSyncWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SyncDeviceInfo> GetSyncDeviceList() {
Log.e(TAG, "[BraveSync] GetSyncDeviceList 000");
ArrayList<SyncDeviceInfo> deviceList = new ArrayList<SyncDeviceInfo>();
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);

Expand All @@ -269,6 +225,4 @@ public ArrayList<SyncDeviceInfo> GetSyncDeviceList() {
private native boolean nativeIsFirstSetupComplete(long nativeBraveSyncWorker);

private native void nativeHandleReset(long nativeBraveSyncWorker);

private native String nativeGetSyncDeviceListJson(long nativeBraveSyncWorker);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<BraveSyncWorker.SyncDeviceInfo> deviceInfos = mainActivity.mBraveSyncWorker.GetSyncDeviceList();
ArrayList<BraveSyncDevices.SyncDeviceInfo> 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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
114 changes: 57 additions & 57 deletions browser/android/brave_sync_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
}

Expand Down Expand Up @@ -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=" <<device_list;
return device_list;
}

base::android::ScopedJavaLocalRef<jstring>
BraveSyncWorker::GetSyncDeviceListJson(JNIEnv* env,
const base::android::JavaParamRef<jobject>& 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<jobject>& jcaller) {
Expand Down
15 changes: 1 addition & 14 deletions browser/android/brave_sync_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -50,21 +48,13 @@ class BraveSyncWorker : public syncer::SyncServiceObserver,
void HandleReset(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller);

base::android::ScopedJavaLocalRef<jstring>
GetSyncDeviceListJson(JNIEnv* env,
const base::android::JavaParamRef<jobject>& 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;

Expand All @@ -73,9 +63,6 @@ class BraveSyncWorker : public syncer::SyncServiceObserver,
ScopedObserver<syncer::SyncService, syncer::SyncServiceObserver>
sync_service_observer_{this};

ScopedObserver<syncer::DeviceInfoTracker, syncer::DeviceInfoTracker::Observer>
device_info_tracker_observer_{this};

DISALLOW_COPY_AND_ASSIGN(BraveSyncWorker);
};

Expand Down
Loading

0 comments on commit 4088d45

Please sign in to comment.