Skip to content

Commit

Permalink
Merge pull request #6732 from brave/sync_remove_other_device
Browse files Browse the repository at this point in the history
Sync: remove other devices from chain
  • Loading branch information
AlexeyBarabash authored Oct 29, 2020
2 parents a1bebd9 + 9b67d15 commit 73b2027
Show file tree
Hide file tree
Showing 40 changed files with 663 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public class BraveSyncScreensPreference extends BravePreferenceFragment
private ImageButton mPasteButton;
private Button mCopyButton;
private Button mAddDeviceButton;
private Button mRemoveDeviceButton;
private Button mShowCategoriesButton;
private Button mQRCodeButton;
private Button mCodeWordsButton;
Expand All @@ -144,7 +143,6 @@ public class BraveSyncScreensPreference extends BravePreferenceFragment
private TextView mBraveSyncAddDeviceCodeWords;
private CameraSource mCameraSource;
private CameraSourcePreview mCameraSourcePreview;
private String mDeviceName = "";
private ListView mDevicesListView;
private ArrayAdapter<String> mDevicesAdapter;
private List<String> mDevicesList;
Expand Down Expand Up @@ -195,6 +193,8 @@ public void onConfigurationChanged(Configuration newConfig) {
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ProfileSyncService.get().addSyncStateChangedListener(this);

InvalidateCodephrase();

if (ensureCameraPermission()) {
Expand Down Expand Up @@ -289,13 +289,7 @@ public void run() {
TextView textView = (TextView) listItemView.findViewById(
R.id.brave_sync_device_text);
if (null != textView) {
textView.setText(device.mName);
}

if (device.mIsCurrentDevice) {
mDeviceName = device.mName;
// Current device is deleted by button on the bottom
if (null != textView) {
if (device.mIsCurrentDevice) {
// Highlight curret device
textView.setTextColor(ApiCompatibilityUtils.getColor(
getActivity().getResources(),
Expand All @@ -304,10 +298,25 @@ public void run() {
+ getResources().getString(
R.string.brave_sync_this_device_text);
textView.setText(currentDevice);
} else {
textView.setText(device.mName);
}
// mRemoveDeviceButton is always visible, we can leave the chain
// in any time with sync v2 (except we are now doing reset)
}

AppCompatImageView deleteButton =
(AppCompatImageView) listItemView.findViewById(
R.id.brave_sync_remove_device);
if (device.mSupportsSelfDelete || device.mIsCurrentDevice) {
deleteButton.setTag(device);
deleteButton.setOnClickListener(v -> {
BraveSyncDevices.SyncDeviceInfo deviceToDelete =
(BraveSyncDevices.SyncDeviceInfo) v.getTag();
deleteDeviceDialog(deviceToDelete);
});
} else {
deleteButton.setVisibility(View.GONE);
}

insertPoint.addView(separator, index++);
insertPoint.addView(listItemView, index++);
}
Expand Down Expand Up @@ -439,11 +448,6 @@ public void onActivityCreated(Bundle savedInstanceState) {
mAddDeviceButton.setOnClickListener(this);
}

mRemoveDeviceButton = (Button) getView().findViewById(R.id.brave_sync_btn_remove_device);
if (null != mRemoveDeviceButton) {
mRemoveDeviceButton.setOnClickListener(this);
}

mShowCategoriesButton =
(Button) getView().findViewById(R.id.brave_sync_btn_show_categories);
if (null != mShowCategoriesButton) {
Expand Down Expand Up @@ -550,9 +554,8 @@ public void onClick(View v) {
&& v != mEnterCodeWordsButton && v != mDoneButton && v != mDoneLaptopButton
&& v != mUseCameraButton && v != mConfirmCodeWordsButton
&& v != mMobileButton && v != mLaptopButton && v != mPasteButton
&& v != mCopyButton && v != mRemoveDeviceButton
&& v != mShowCategoriesButton && v != mAddDeviceButton && v != mQRCodeButton
&& v != mCodeWordsButton))
&& v != mCopyButton && v != mShowCategoriesButton && v != mAddDeviceButton
&& v != mQRCodeButton && v != mCodeWordsButton))
return;

if (mScanChainCodeButton == v) {
Expand Down Expand Up @@ -683,8 +686,6 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
}
});
}
} else if (mRemoveDeviceButton == v) {
deleteDeviceDialog(mDeviceName);
} else if (mShowCategoriesButton == v) {
SettingsLauncher settingsLauncher = new SettingsLauncherImpl();
settingsLauncher.launchSettingsActivity(getContext(), BraveManageSyncSettings.class);
Expand Down Expand Up @@ -877,6 +878,9 @@ public void onDestroy() {
if (mCameraSourcePreview != null) {
mCameraSourcePreview.release();
}

ProfileSyncService.get().removeSyncStateChangedListener(this);

if (deviceInfoObserverSet) {
BraveSyncDevices.get().removeDeviceInfoChangedListener(this);
deviceInfoObserverSet = false;
Expand Down Expand Up @@ -949,8 +953,7 @@ public void onClick(DialogInterface dialog, int button) {}
alertDialog.show();
}

private void deleteDeviceDialog(String deviceName) {
Log.v(TAG, "deleteDeviceDialog deviceName=" + deviceName);
private void deleteDeviceDialog(BraveSyncDevices.SyncDeviceInfo device) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity(), R.style.Theme_Chromium_AlertDialog);
if (null == alert) {
return;
Expand All @@ -960,14 +963,21 @@ private void deleteDeviceDialog(String deviceName) {
public void onClick(DialogInterface dialog, int button) {
if (button == AlertDialog.BUTTON_POSITIVE) {
if (getBraveSyncWorker() != null) {
getBraveSyncWorker().ResetSync();
startLeaveSyncChainOperations();
if (device.mIsCurrentDevice) {
getBraveSyncWorker().ResetSync();
startLeaveSyncChainOperations();
} else {
BraveSyncDevices.get().DeleteDevice(device.mGuid);
}
}
}
}
};
String deviceNameToDisplay =
deviceName + " " + getResources().getString(R.string.brave_sync_this_device_text);
String deviceNameToDisplay = device.mName;
if (device.mIsCurrentDevice) {
deviceNameToDisplay = deviceNameToDisplay + " "
+ getResources().getString(R.string.brave_sync_this_device_text);
}
AlertDialog alertDialog =
alert.setTitle(getResources().getString(R.string.brave_sync_remove_device_text))
.setMessage(
Expand All @@ -990,9 +1000,6 @@ private void startLeaveSyncChainOperations() {

mShowCategoriesButton.setVisibility(View.GONE);
mAddDeviceButton.setVisibility(View.GONE);
mRemoveDeviceButton.setVisibility(View.GONE);

ProfileSyncService.get().addSyncStateChangedListener(this);

PostTask.postDelayedTask(
UiThreadTaskTraits.USER_VISIBLE, () -> leaveSyncChainComplete(), 5 * 1000);
Expand All @@ -1007,20 +1014,22 @@ private void leaveSyncChainComplete() {
if (mLeaveSyncChainInProgress) {
mLeaveSyncChainInProgress = false;

ProfileSyncService.get().removeSyncStateChangedListener(this);

mShowCategoriesButton.setVisibility(View.VISIBLE);
mAddDeviceButton.setVisibility(View.VISIBLE);
mRemoveDeviceButton.setVisibility(View.VISIBLE);

setAppropriateView();
}
}

@Override
public void syncStateChanged() {
if (ProfileSyncService.get().isFirstSetupComplete() == false && mLeaveSyncChainInProgress) {
leaveSyncChainComplete();
if (ProfileSyncService.get().isFirstSetupComplete() == false) {
if (mLeaveSyncChainInProgress) {
leaveSyncChainComplete();
} else {
InvalidateCodephrase();
setAppropriateView();
}
}
}

Expand Down Expand Up @@ -1239,10 +1248,6 @@ private void setSyncDoneLayout() {
return;
}

boolean firstSetupComplete = getBraveSyncWorker().IsFirstSetupComplete();
getBraveSyncWorker().SaveCodephrase(GetCodephrase());
getBraveSyncWorker().FinalizeSyncSetup();

if (!deviceInfoObserverSet) {
BraveSyncDevices.get().addDeviceInfoChangedListener(this);
deviceInfoObserverSet = true;
Expand Down Expand Up @@ -1275,8 +1280,7 @@ private void setSyncDoneLayout() {
adjustWidth(mScrollViewSyncDone, false);
mScrollViewSyncDone.setVisibility(View.VISIBLE);
}
// With sync v2 we may leave the sync chain even if we don't see other
// devices in chain, mRemoveDeviceButton is always visible

BraveActivity mainActivity = BraveActivity.getBraveActivity();
if (null != mainActivity) {
mBraveSyncTextDevicesTitle.setText(getResources().getString(R.string.brave_sync_loading_devices_title));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ protected void deviceInfoChanged() {
public class SyncDeviceInfo {
public String mName;
public boolean mIsCurrentDevice;
public boolean mSupportsSelfDelete;
public String mType;
public Date mLastUpdatedTimestamp;
public String mGuid;
}

public ArrayList<SyncDeviceInfo> GetSyncDeviceList() {
Expand All @@ -121,18 +123,25 @@ public ArrayList<SyncDeviceInfo> GetSyncDeviceList() {
deviceInfo.mType = device.getString("type");
long lastUpdatedTimestamp = device.getLong("lastUpdatedTimestamp");
deviceInfo.mLastUpdatedTimestamp = new Date(lastUpdatedTimestamp);
deviceInfo.mGuid = device.getString("guid");
deviceInfo.mSupportsSelfDelete = device.getBoolean("supportsSelfDelete");
deviceList.add(deviceInfo);
}
} catch (JSONException e) {
Log.e(TAG, "GetDeviceNameByObjectId JSONException error " + e);
Log.e(TAG, "GetSyncDeviceList JSONException error " + e);
} catch (IllegalStateException e) {
Log.e(TAG, "GetDeviceNameByObjectId IllegalStateException error " + e);
Log.e(TAG, "GetSyncDeviceList IllegalStateException error " + e);
}
return deviceList;
}

public void DeleteDevice(String deviceGuid) {
nativeDeleteDevice(mNativeBraveSyncDevicesAndroid, deviceGuid);
}

private native void nativeInit();
private native void nativeDestroy(long nativeBraveSyncDevicesAndroid);

private native String nativeGetSyncDeviceListJson(long nativeBraveSyncDevicesAndroid);
private native void nativeDeleteDevice(long nativeBraveSyncDevicesAndroid, String deviceGuid);
}
12 changes: 12 additions & 0 deletions android/java/res/layout/brave_sync_device.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
android:textSize="16sp"
android:layout_centerVertical="true"
android:layout_toEndOf="@+id/brave_sync_device_image"
android:layout_toStartOf="@+id/brave_sync_remove_device"
android:paddingStart="10dip" />

<androidx.appcompat.widget.AppCompatImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/brave_sync_remove_device"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/ListItemEndIcon"
android:contentDescription="@string/remove"
android:src="@drawable/ic_delete_white_24dp"
app:tint="@color/default_icon_color_tint_list"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
15 changes: 4 additions & 11 deletions android/java/res/layout/brave_sync_done.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,18 @@
android:layout_width="match_parent"
android:visibility="invisible" />

<Button android:id="@+id/brave_sync_btn_show_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/brave_sync_categories_text"
style="@style/BraveSyncButton" />

<Button android:id="@+id/brave_sync_btn_add_device"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/brave_sync_add_device_text"
style="@style/BraveSyncButton" />

<Button android:id="@+id/brave_sync_btn_remove_device"
<Button android:id="@+id/brave_sync_btn_show_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/brave_sync_remove_device_text"
style="@style/BraveSyncBottomButton"
android:text="@string/brave_sync_categories_text"
style="@style/BraveSyncButton"
android:background="@android:color/transparent"
android:textColor="@color/google_red_600" />

android:textColor="@color/modern_grey_600" />
</LinearLayout>
</ScrollView>
6 changes: 6 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_BRAVE_SYNC_DEVICE_LIST_LAST_ACTIVE_COLUMN" desc="Brave Sync device list table column title for last active date">
Last Active
</message>
<message name="IDS_SETTINGS_BRAVE_SYNC_DEVICE_LIST_REMOVE_COLUMN" desc="Brave Sync device list table column title for remove other device buttons">
Remove
</message>
<message name="IDS_BRAVE_SYNC_SETUP_TITLE" desc="The Brave Sync title.">
Sync Setup
</message>
Expand Down Expand Up @@ -606,6 +609,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_BRAVE_SYNC_RESET_CONFIRMATION" desc="Confirmation message prompt to make sure the user is ok with leaving sync">
If you reset Sync, you will have to re-enter a sync code from another device in order to sync with it. Are you sure you want to proceed?
</message>
<message name="IDS_BRAVE_SYNC_DELETE_DEVICE_CONFIRMATION" desc="Confirmation message prompt to make sure the user is ok to delete other device from the chain">
Are you sure you want to delete selected device from sync chain?
</message>
<!-- Sync Infobar -->
<message name="IDS_BRAVE_SYNC_V2_MIGRATE_INFOBAR_MESSAGE" desc="Message text shown to users who used a previous version of Brave Sync and need to perform setup again for the new version.">
Brave Sync has been upgraded and requires setup
Expand Down
6 changes: 5 additions & 1 deletion browser/resources/settings/brave_icons.html
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
<g id="clipboard" viewBox="0 0 32 32">
<path d="M16 2c-1.645 0-3 1.355-3 3H9C7.355 5 6 6.355 6 8v18c0 1.645 1.355 3 3 3h14c1.645 0 3-1.355 3-3V8c0-1.645-1.355-3-3-3h-4c0-1.645-1.355-3-3-3zm0 2c.564 0 1 .436 1 1 0 .564-.436 1-1 1-.564 0-1-.436-1-1 0-.564.436-1 1-1zM9 7h4v1a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1V7h4c.565 0 1 .435 1 1v18c0 .565-.435 1-1 1H9c-.565 0-1-.435-1-1V8c0-.565.435-1 1-1zm3 6a1 1 0 1 0 0 2h8a1 1 0 1 0 0-2h-8zm0 4a1 1 0 1 0 0 2h5a1 1 0 1 0 0-2h-5zm0 4a1 1 0 1 0 0 2h8a1 1 0 1 0 0-2h-8z" />
</g>
<g id="close-circle" viewBox="0 0 32 32">
<path d="M16 3a13 13 0 1 0 13 13A13 13 0 0 0 16 3zm0 24a11 11 0 1 1 11-11 11 11 0 0 1-11 11z" />
<path d="M20.71 11.29a1 1 0 0 0-1.42 0L16 14.59l-3.29-3.3a1 1 0 0 0-1.42 1.42l3.3 3.29-3.3 3.29a1 1 0 0 0 0 1.42 1 1 0 0 0 1.42 0l3.29-3.3 3.29 3.3a1 1 0 0 0 1.42 0 1 1 0 0 0 0-1.42L17.41 16l3.3-3.29a1 1 0 0 0 0-1.42z" />
</g>
<g id="full-color-brave-lion" viewBox="0 0 24 24">
<path fill="#FB542B"
d="M20.856 8.11l-.682-1.84.474-1.057a.36.36 0 0 0-.074-.402l-1.29-1.296a2.093 2.093 0 0 0-2.163-.5l-.361.124L14.876 1H7.95L6.09 3.165l-.35-.123a2.098 2.098 0 0 0-2.184.505l-1.313 1.32a.286.286 0 0 0-.058.32l.495 1.098L2 8.125l.44 1.659 2 7.56a3.85 3.85 0 0 0 1.501 2.158s2.43 1.702 4.828 3.249c.211.136.432.235.668.232.237.003.458-.096.668-.233 2.693-1.754 4.824-3.255 4.824-3.255a3.852 3.852 0 0 0 1.498-2.16l1.992-7.563.437-1.661z" />
Expand All @@ -87,4 +91,4 @@
</g>
</defs>
</svg>
</iron-iconset-svg>
</iron-iconset-svg>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export class BraveSyncBrowserProxy {
resetSyncChain() {
return sendWithPromise('SyncSetupReset');
}
deleteDevice(deviceId) {
return sendWithPromise('SyncDeleteDevice', deviceId);
}
}

addSingletonGetter(BraveSyncBrowserProxy);
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@
justify-content: end;
}

.delete-button
{
margin: 0;
width: 24px;
height: 24px;
}

section
{
margin-top: 20px;
Expand All @@ -82,6 +89,7 @@ <h1>$i18n{braveSyncDeviceListTitle}</h1>
<span class="device-name">$i18n{braveSyncDeviceListNameColumn}</span>
</th>
<th scope="col">$i18n{braveSyncDeviceListLastActiveColumn}</th>
<th scope="col">$i18n{braveSyncDeviceListRemoveColumn}</th>
</tr>
</thead>
<tbody>
Expand All @@ -96,6 +104,15 @@ <h1>$i18n{braveSyncDeviceListTitle}</h1>
</template>
</th>
<td>[[getDeviceDisplayDate(item)]]</td>
<td>
<template is="dom-if" if="[[item.supportsSelfDelete]]">
<cr-icon-button
class="delete-button"
iron-icon="brave_settings:close-circle"
on-click="onDeleteDevice_" arg="[[item.guid]]"
aria-label="Delete selected device" />
</template>
</td>
</tr>
</template>
</tbody>
Expand Down
11 changes: 11 additions & 0 deletions browser/resources/settings/brave_sync_page/brave_sync_configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,16 @@ Polymer({
this.syncCode = undefined
const router = Router.getInstance();
router.navigateTo(router.getRoutes().BRAVE_SYNC);
},

onDeleteDevice_: async function(e) {
const messageText = this.i18n('braveSyncDeleteDeviceConfirmation')
const shouldDeleteDevice = confirm(messageText)
if (!shouldDeleteDevice) {
return
}

const deviceGuid = e.currentTarget.arg
await this.browserProxy_.deleteDevice(deviceGuid);
}
});
Loading

0 comments on commit 73b2027

Please sign in to comment.