Skip to content

Commit

Permalink
WIP - display device list
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyBarabash committed Jun 9, 2020
1 parent fc22645 commit 628bb08
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 7 deletions.
55 changes: 51 additions & 4 deletions android/java/org/chromium/chrome/browser/BraveSyncWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.Date;
//import java.util.HashSet;
//import java.util.HashMap;
//import java.util.Random;
//import java.util.Scanner;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -207,6 +208,50 @@ 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 @@ -224,4 +269,6 @@ public void HandleReset() {
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 @@ -33,6 +33,7 @@
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
Expand Down Expand Up @@ -429,6 +430,7 @@ public void run() {
@Override
public void onDevicesAvailable() {
try {
Log.e(TAG, "onDevicesAvailable 000");
if (null == getActivity()) {
return;
}
Expand All @@ -439,6 +441,63 @@ 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();
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) {
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) {
TextView textView = (TextView) listItemView.findViewById(R.id.brave_sync_device_text);
if (null != textView) {
textView.setText(device.mName);
Log.e(TAG, "[BraveSync] onDevicesAvailable device.mName="+device.mName);
}
// TODO(alexey): remove R.id.brave_sync_remove_device from xml, because we cannot remove other devices with sync v2
AppCompatImageView deleteButton = (AppCompatImageView) listItemView.findViewById(R.id.brave_sync_remove_device);
if (null != deleteButton) {
deleteButton.setVisibility(View.GONE);
}

//if (currentDeviceId.equals(device.mDeviceId)) {
if (device.mIsCurrentDevice) {
// Current device is deleted by button on the bottom
//deleteButton.setVisibility(View.GONE);
if (null != textView) {
// Highlight curret device
textView.setTextColor(ApiCompatibilityUtils.getColor(getActivity().getResources(), R.color.brave_theme_color));
String currentDevice = device.mName + " " + getResources().getString(R.string.brave_sync_this_device_text);
textView.setText(currentDevice);
}
// if (null != mRemoveDeviceButton) {
// mRemoveDeviceButton.setTag(device);
// mRemoveDeviceButton.setVisibility(View.VISIBLE);
// mRemoveDeviceButton.setEnabled(true);
// }
} else {
// deleteButton.setTag(device);
// deleteButton.setOnClickListener(v -> {
// BraveSyncWorker.ResolvedRecordToApply deviceToDelete = (BraveSyncWorker.ResolvedRecordToApply) v.getTag();
// deleteDeviceDialog(deviceToDelete.mDeviceName, deviceToDelete.mDeviceId, deviceToDelete.mObjectId, v);
// });
}
insertPoint.addView(separator, index++);
insertPoint.addView(listItemView, index++);
}
}

if (index > 0) {
mBraveSyncTextDevicesTitle.setText(getResources().getString(R.string.brave_sync_devices_title));
View separator = (View) mInflater.inflate(R.layout.menu_separator, null);
if (null != insertPoint && null != separator) {
insertPoint.addView(separator, index++);
}
}

// SharedPreferences sharedPref = getActivity().getApplicationContext().getSharedPreferences(BraveSyncWorker.PREF_NAME, 0);
// String currentDeviceId = sharedPref.getString(BraveSyncWorker.PREF_DEVICE_ID, "");
// Load other devices in chain
Expand Down
85 changes: 83 additions & 2 deletions browser/android/brave_sync_worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,24 @@

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/json/json_writer.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"

#include "base/path_service.h"
#include "brave/build/android/jni_headers/BraveSyncWorker_jni.h"
#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/brave_sync/crypto/crypto.h"

#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 @@ -125,13 +129,29 @@ void BraveSyncWorker::HandleShowSetupUI(JNIEnv* env,
sync_service_observer_.Add(service);
}

syncer::DeviceInfoTracker* tracker =
DeviceInfoSyncServiceFactory::GetForProfile(profile_)
->GetDeviceInfoTracker();
DCHECK(tracker);
if (tracker) {
device_info_tracker_observer_.Add(tracker);
}

// Mark Sync as requested by the user. It might already be requested, but
// it's not if this is either the first time the user is setting up Sync, or
// Sync was set up but then was reset via the dashboard. This also pokes the
// SyncService to start up immediately, i.e. bypass deferred startup.
if (service) {
service->GetUserSettings()->SetSyncRequested(true);
}

/* TODO: look on
void ProfileSyncServiceAndroid::RequestStart(JNIEnv* env,
const JavaParamRef<jobject>&) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
sync_service_->GetUserSettings()->SetSyncRequested(true);
}
*/
}

// See PeopleHandler::MarkFirstSetupComplete
Expand Down Expand Up @@ -169,6 +189,14 @@ DLOG(ERROR) << "[BraveSync] " << __func__ << " 000";

bool BraveSyncWorker::IsFirstSetupComplete(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jcaller) {
/* TODO: look on
jboolean ProfileSyncServiceAndroid::IsFirstSetupComplete(
JNIEnv* env,
const JavaParamRef<jobject>& obj) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
return sync_service_->GetUserSettings()->IsFirstSetupComplete();
}
*/
syncer::SyncService* sync_service = GetSyncService();
DLOG(ERROR) << "[BraveSync] " << __func__ << " will ret " <<
(sync_service && sync_service->GetUserSettings()->IsFirstSetupComplete());
Expand All @@ -183,6 +211,13 @@ DLOG(ERROR) << "[BraveSync] " << __func__ << " 000";
syncer::SyncService* sync_service = GetSyncService();
DLOG(ERROR) << "[BraveSync] " << __func__ << " sync_service=" << sync_service;
if (sync_service) {
/* TODO: look on
void ProfileSyncServiceAndroid::RequestStop(JNIEnv* env,
const JavaParamRef<jobject>&) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
sync_service_->GetUserSettings()->SetSyncRequested(false);
}
*/
sync_service->GetUserSettings()->SetSyncRequested(false);
sync_service->StopAndClear();
}
Expand Down Expand Up @@ -328,6 +363,52 @@ 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
17 changes: 16 additions & 1 deletion browser/android/brave_sync_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#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 @@ -18,7 +19,8 @@ class Profile;
namespace chrome {
namespace android {

class BraveSyncWorker : public syncer::SyncServiceObserver {
class BraveSyncWorker : public syncer::SyncServiceObserver,
public syncer::DeviceInfoTracker::Observer {
public:
BraveSyncWorker(JNIEnv* env,
//jobject obj
Expand Down Expand Up @@ -48,20 +50,33 @@ 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;

std::string passphrase_;

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);
};

} // namespace android
Expand Down

0 comments on commit 628bb08

Please sign in to comment.