Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Device Name in Firefox/Mozilla Sync #1071

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/common/shared/com/igalia/wolvic/browser/Accounts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.future
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import mozilla.components.concept.sync.*
import mozilla.components.service.fxa.FirefoxAccount
import mozilla.components.service.fxa.SyncEngine
Expand Down Expand Up @@ -131,6 +132,10 @@ class Accounts constructor(val context: Context) {

accountStatus = AccountStatus.SIGNED_IN

// We must delay applying the device name from settings after we are authenticated
// as we will stuck if we get it directly when initializing services.accountManager
runBlocking { setDeviceName(SettingsStore.getInstance(context).deviceName) }

// Enable syncing after signing in
syncNowAsync(SyncReason.EngineChange, true)

Expand Down Expand Up @@ -397,4 +402,8 @@ class Accounts constructor(val context: Context) {
originSessionId = sessionId
}

suspend fun setDeviceName(deviceName: String) {
services.accountManager.authenticatedAccount()?.deviceConstellation()?.setDeviceName(deviceName, context);
}

}
5 changes: 3 additions & 2 deletions app/src/common/shared/com/igalia/wolvic/browser/Services.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package com.igalia.wolvic.browser
import android.content.Context
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.lifecycle.ProcessLifecycleOwner
import com.igalia.wolvic.BuildConfig
import com.igalia.wolvic.R
Expand Down Expand Up @@ -92,8 +93,8 @@ class Services(val context: Context, places: Places): WSession.NavigationDelegat
serverConfig = serverConfig,
deviceConfig = DeviceConfig(
// This is a default name, and can be changed once user is logged in.
// E.g. accountManager.authenticatedAccount()?.deviceConstellation()?.setDeviceNameAsync("new name")
name = "${context.getString(R.string.app_name)} on ${Build.MANUFACTURER} ${Build.MODEL}",
HollowMan6 marked this conversation as resolved.
Show resolved Hide resolved
// E.g. accountManager.authenticatedAccount()?.deviceConstellation()?.setDeviceName("new name", context)
name = com.igalia.wolvic.utils.DeviceType.getDeviceName(context),
type = DeviceType.VR,
capabilities = setOf(DeviceCapability.SEND_TAB)
),
Expand Down
11 changes: 11 additions & 0 deletions app/src/common/shared/com/igalia/wolvic/browser/SettingsStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,17 @@ public float getWindowAspect() {
return (float)getWindowWidth() / (float)getWindowHeight();
}

public String getDeviceName() {
return mPrefs.getString(
mContext.getString(R.string.settings_key_device_name), DeviceType.getDeviceName(mContext));
}

public void setDeviceName(String aDeviceName) {
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString(mContext.getString(R.string.settings_key_device_name), aDeviceName);
editor.commit();
}

public int getDisplayDpi() {
return mPrefs.getInt(
mContext.getString(R.string.settings_key_display_dpi), DISPLAY_DPI_DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@
import com.igalia.wolvic.ui.widgets.UIWidget;
import com.igalia.wolvic.ui.widgets.WidgetManagerDelegate;
import com.igalia.wolvic.ui.widgets.dialogs.SignOutDialogWidget;
import com.igalia.wolvic.utils.DeviceType;
import com.igalia.wolvic.utils.SystemUtils;

import org.jetbrains.annotations.NotNull;

import java.util.Objects;
import java.util.concurrent.Executor;

import kotlin.Unit;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.EmptyCoroutineContext;
import mozilla.components.concept.sync.AccountObserver;
import mozilla.components.concept.sync.AuthFlowError;
import mozilla.components.concept.sync.AuthType;
Expand Down Expand Up @@ -78,6 +83,12 @@ protected void updateUI() {
mBinding.signButton.setOnClickListener(this::signOut);
mBinding.syncButton.setOnClickListener(this::sync);

mBinding.deviceNameEdit.setHint1(DeviceType.getDeviceName(getContext()));
mBinding.deviceNameEdit.setDefaultFirstValue(DeviceType.getDeviceName(getContext()));
mBinding.deviceNameEdit.setFirstText(SettingsStore.getInstance(getContext()).getDeviceName());
mBinding.deviceNameEdit.setOnClickListener(mDeviceNameListener);
setDeviceName(SettingsStore.getInstance(getContext()).getDeviceName(), false);

mBinding.setIsSyncing(mAccounts.isSyncing());
mBinding.setLastSync(mAccounts.lastSync());

Expand All @@ -91,6 +102,34 @@ protected void updateUI() {
mBinding.footerLayout.setFooterButtonClickListener(v -> resetOptions());
}

@Override
protected void onDismiss() {
if (isEditing()) {
return;
}
super.onDismiss();
}

@Override
public boolean isEditing() {
boolean editing = mBinding.deviceNameEdit.isEditing();
if (editing) {
mBinding.deviceNameEdit.cancel();
}

return editing;
HollowMan6 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public void onGlobalFocusChanged(View oldFocus, View newFocus) {
if (oldFocus == null) {
return;
}
if (mBinding.deviceNameEdit.contains(oldFocus) && mBinding.deviceNameEdit.isEditing()) {
mBinding.deviceNameEdit.cancel();
}
}

@Override
public void onShown() {
super.onShown();
Expand All @@ -107,6 +146,9 @@ public void onShown() {

@Override
public void onHidden() {
if (isEditing()) {
return;
}
super.onHidden();

mAccounts.removeAccountListener(mAccountListener);
Expand All @@ -132,6 +174,7 @@ private void resetOptions() {
mBinding.bookmarksSyncSwitch.setValue(SettingsStore.BOOKMARKS_SYNC_DEFAULT, true);
mBinding.historySyncSwitch.setValue(SettingsStore.HISTORY_SYNC_DEFAULT, true);
mBinding.loginsSyncSwitch.setValue(SettingsStore.LOGIN_SYNC_DEFAULT, true);
setDeviceName(DeviceType.getDeviceName(getContext()), true);
}

private SyncStatusObserver mSyncListener = new SyncStatusObserver() {
Expand Down Expand Up @@ -169,6 +212,32 @@ private void updateSyncBindings(boolean isSyncing) {
mBinding.executePendingBindings();
}

private void setDeviceName(String newDeviceName, boolean doApply) {
mBinding.deviceNameEdit.setOnClickListener(null);
if (newDeviceName.isEmpty()) {
newDeviceName = DeviceType.getDeviceName(getContext());
}
if (doApply) {
String prevDeviceName = SettingsStore.getInstance(getContext()).getDeviceName();
if (!Objects.equals(prevDeviceName, newDeviceName)) {
SettingsStore.getInstance(getContext()).setDeviceName(newDeviceName);
Continuation<? super Unit> completion = new Continuation<Unit>() {
@NonNull
@Override
public CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}

@Override
public void resumeWith(@NonNull Object o) {}
};
mAccounts.setDeviceName(newDeviceName, completion);
}
}
mBinding.deviceNameEdit.setFirstText(newDeviceName);
mBinding.deviceNameEdit.setOnClickListener(mDeviceNameListener);
}

void updateCurrentAccountState() {
switch(mAccounts.getAccountStatus()) {
case NEEDS_RECONNECT:
Expand Down Expand Up @@ -225,6 +294,11 @@ private void signOut(View view) {
mSignOutDialog.show(UIWidget.REQUEST_FOCUS);
}

private OnClickListener mDeviceNameListener = (view) -> {
String newDeviceName = mBinding.deviceNameEdit.getFirstText();
setDeviceName(newDeviceName, true);
};

private AccountObserver mAccountListener = new AccountObserver() {

@Override
Expand Down
49 changes: 31 additions & 18 deletions app/src/common/shared/com/igalia/wolvic/utils/DeviceType.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.igalia.wolvic.utils;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.IntDef;

import com.igalia.wolvic.R;
import com.igalia.wolvic.BuildConfig;

public class DeviceType {
Expand All @@ -30,60 +34,60 @@ public class DeviceType {
public static final int MetaQuest3 = 18;

private static @Type int mType = Unknown;
private static String mDeviceName = "Unknown Device";

public static void setType(@Type int aType) {
String name;
switch (aType) {
case OculusGo:
name = "Oculus Go";
mDeviceName = "Oculus Go";
break;
case OculusQuest:
name = "Oculus Quest";
mDeviceName = "Oculus Quest";
break;
case OculusQuest2:
name = "Oculus Quest 2";
mDeviceName = "Oculus Quest 2";
break;
case MetaQuestPro:
name = "Meta Quest Pro";
mDeviceName = "Meta Quest Pro";
break;
case ViveFocus:
name = "Vive Focus";
mDeviceName = "Vive Focus";
break;
case ViveFocusPlus:
name = "Vive Focus Plus";
mDeviceName = "Vive Focus Plus";
break;
case PicoNeo2:
name = "Pico Neo 2";
mDeviceName = "Pico Neo 2";
break;
case PicoNeo3:
name = "Pico Neo 3";
mDeviceName = "Pico Neo 3";
break;
case PicoG2:
name = "Pico G2";
mDeviceName = "Pico G2";
break;
case PicoXR:
name = "Pico XR";
mDeviceName = "Pico XR";
break;
case LynxR1:
name = "Lynx-R1";
mDeviceName = "Lynx-R1";
break;
case LenovoA3:
name = "Lenovo A3";
mDeviceName = "Lenovo A3";
break;
case LenovoVRX:
name = "Lenovo VRX";
mDeviceName = "Lenovo VRX";
break;
case MagicLeap2:
name = "Magic Leap 2";
mDeviceName = "Magic Leap 2";
break;
case MetaQuest3:
name = "Meta Quest 3";
mDeviceName = "Meta Quest 3";
break;
default:
name = "Unknown Type";
mDeviceName = "Unknown Device";
break;
}
Log.d("VRB", "Setting device type to: " + name);
Log.d("VRB", "Setting device type to: " + mDeviceName);
mType = aType;
}
public static @Type int getType() {
Expand Down Expand Up @@ -146,4 +150,13 @@ else if (BuildConfig.FLAVOR_store.toLowerCase().contains("applab"))
else
return StoreType.NONE;
}

public static String getDeviceName(Context aContext) {
String appName = aContext.getString(R.string.app_name);
String deviceName = mDeviceName;
if (mType == DeviceType.Unknown) {
deviceName = Build.MANUFACTURER + " " + Build.MODEL;
}
return aContext.getString(R.string.device_name, appName, deviceName);
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/options_fxa_account.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@
android:text=""
tools:text="mozilla@mozilla.com" />

<com.igalia.wolvic.ui.views.settings.SingleEditSetting
android:id="@+id/device_name_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:width="300dp"
android:inputType="textWebEditText"
app:description="@string/fxa_account_options_device_name"
app:hintTextColor="@color/iron_blur"
app:highlightedTextColor="@color/fog" />

<TextView
style="@style/settingsDescription"
android:gravity="center_vertical"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values-en-rGB/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,9 @@
<!-- This string is used to in the FxA account manage panel as a description of the Sign In/Out button. -->
<string name="fxa_account_options_account">Account</string>

<!-- This string is used to in the FxA account manage panel as a description of the device name edit. -->
<string name="fxa_account_options_device_name">Device Name</string>

<!-- This string is used to in the FxA account manage panel as a header for the sync options. -->
<string name="fxa_account_options_sync_title">Sync Settings</string>

Expand Down Expand Up @@ -1708,6 +1711,9 @@
'%1$s' will be replaced at runtime with the app's name. -->
<string name="homepage_hint">%1$s Home (Default)</string>

<!-- This string is displayed in mozilla account's device name.
'%1$s' will be replaced at runtime with the app's name while '%2$s' will be replaced with the actual device name. -->
<string name="device_name">%1$s on %2$s</string>

<!-- This string is displayed in the title of an authentication prompt, which requests a username and a password. -->
<string name="authentication_required">Authentication Required</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,8 @@
<string name="developer_options_homepage">主页</string>
<!-- This string is used to in the FxA account manage panel as a description of the Sign In/Out button. -->
<string name="fxa_account_options_account">账户</string>
<!-- This string is used to in the FxA account manage panel as a description of the device name edit. -->
<string name="fxa_account_options_device_name">设备名称</string>
<!-- This string is used to in the FxA account manage panel as a header for the sync options. -->
<string name="fxa_account_options_sync_title">同步设置</string>
<!-- This string is used to in the FxA account manage panel as the description of the Sync button when is not syncing.
Expand Down Expand Up @@ -1271,6 +1273,9 @@
<!-- This string is displayed in any button used for removing all the items in the current context.
'%1$s' will be replaced at runtime with the app's name. -->
<string name="homepage_hint">%1$s 主页(默认)</string>
<!-- This string is displayed in mozilla account's device name.
'%1$s' will be replaced at runtime with the app's name while '%2$s' will be replaced with the actual device name. -->
<string name="device_name">%2$s 上的 %1$s</string>
<!-- This string is displayed in the title of an authentication prompt, which requests a username and a password. -->
<string name="authentication_required">需要验证身份</string>
<!-- This string is displayed as the label of a username input in an authentication prompt. -->
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,9 @@
<!-- This string is used to in the FxA account manage panel as a description of the Sign In/Out button. -->
<string name="fxa_account_options_account">帳號</string>

<!-- This string is used to in the FxA account manage panel as a description of the device name edit. -->
<string name="fxa_account_options_device_name">裝置名稱</string>

<!-- This string is used to in the FxA account manage panel as a header for the sync options. -->
<string name="fxa_account_options_sync_title">同步設定</string>

Expand Down Expand Up @@ -1706,7 +1709,9 @@
<!-- This string is displayed in any button used for removing all the items in the current context.
'%1$s' will be replaced at runtime with the app's name. -->
<string name="homepage_hint">%1$s 首頁(預設值)</string>

<!-- This string is displayed in mozilla account's device name.
'%1$s' will be replaced at runtime with the app's name while '%2$s' will be replaced with the actual device name. -->
<string name="device_name">%2$s 上的 %1$s</string>
<!-- This string is displayed in the title of an authentication prompt, which requests a username and a password. -->
<string name="authentication_required">需要驗證</string>
<!-- This string is displayed as the label of a username input in an authentication prompt. -->
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@

<!-- Options Panel -->
<dimen name="options_width">585dp</dimen>
<dimen name="options_height">385dp</dimen>
<dimen name="options_height">420dp</dimen>
HollowMan6 marked this conversation as resolved.
Show resolved Hide resolved

<!-- Developer Panel -->
<dimen name="options_button_width">140dp</dimen>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/non_L10n.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<string name="settings_key_user_agent_version" translatable="false">settings_user_agent_version_v2</string>
<string name="settings_key_input_mode" translatable="false">settings_touch_mode</string>
<string name="settings_key_display_density" translatable="false">settings_display_density</string>
<string name="settings_key_display_dpi" translatable="false">settings_display_dpi</string>>
<string name="settings_key_display_dpi" translatable="false">settings_display_dpi</string>
<string name="settings_key_device_name" translatable="false">settings_device_name</string>
<string name="settings_key_env" translatable="false">settings_env</string>
<string name="settings_key_pointer_color" translatable="false">settings_pointer_color</string>
<string name="settings_key_scroll_direction" translatable="false">settings_scroll_direction</string>
Expand Down
Loading