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

fix: replace classic mini homescreen image #5854

Merged
merged 12 commits into from
Sep 23, 2024
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { ResourceType, type Success } from '@onekeyfe/hd-transport';
import { isNil } from 'lodash';

import type { IHardwareHomeScreenName } from '@onekeyhq/kit/src/views/AccountManagerStacks/pages/HardwareHomeScreen/hardwareHomeScreenData';
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
import { backgroundMethod } from '@onekeyhq/shared/src/background/backgroundDecorators';
import { FirmwareVersionTooLow } from '@onekeyhq/shared/src/errors';
import { convertDeviceResponse } from '@onekeyhq/shared/src/errors/utils/deviceErrorUtils';
import {
CoreSDKLoader,
generateConnectSrc,
} from '@onekeyhq/shared/src/hardware/instance';
import deviceHomeScreenUtils from '@onekeyhq/shared/src/utils/deviceHomeScreenUtils';
import deviceUtils from '@onekeyhq/shared/src/utils/deviceUtils';
import type { EOnekeyDomain } from '@onekeyhq/shared/types';

Expand Down Expand Up @@ -37,7 +39,7 @@ export type ISetDeviceLabelParams = { walletId: string; label: string };
export type ISetDeviceHomeScreenParams = {
// TODO use IHardwareHomeScreenData
dbDeviceId: string;
imgName: string;
imgName: IHardwareHomeScreenName;
imgHex: string;
thumbnailHex: string;
isUserUpload?: boolean;
Expand Down Expand Up @@ -166,7 +168,11 @@ export class DeviceSettingsManager extends ServiceHardwareManagerBase {

return this.backgroundApi.serviceHardwareUI.withHardwareProcessing(
async () => {
if (isUserUpload) {
const isMonochrome = deviceHomeScreenUtils.isMonochromeScreen(
device.deviceType,
);
// pro touch upload image
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
if (isUserUpload && !isMonochrome) {
const hardwareSDK = await this.getSDKInstance();
const uploadResParams: DeviceUploadResourceParams = {
resType: ResourceType.WallPaper,
Expand All @@ -182,8 +188,13 @@ export class DeviceSettingsManager extends ServiceHardwareManagerBase {
} else {
const { getHomeScreenHex } = await CoreSDKLoader();
const deviceType = device.deviceType;
const internalHex = getHomeScreenHex(deviceType, imgName);
// eslint-disable-next-line no-param-reassign
imgHex = imgHex || getHomeScreenHex(deviceType, imgName);
imgHex = imgHex || internalHex;
if (imgName === 'blank') {
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line no-param-reassign
imgHex = '';
}
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
if (!imgHex) {
// empty string will clear the home screen(classic,mini)
// throw new Error('Invalid home screen hex');
Expand Down
19 changes: 15 additions & 4 deletions packages/kit-bg/src/services/ServiceHardware/ServiceHardware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ import platformEnv from '@onekeyhq/shared/src/platformEnv';
import accountUtils from '@onekeyhq/shared/src/utils/accountUtils';
import { checkIsDefined } from '@onekeyhq/shared/src/utils/assertUtils';
import cacheUtils, { memoizee } from '@onekeyhq/shared/src/utils/cacheUtils';
import deviceHomeScreenUtils, {
DEFAULT_T1_HOME_SCREEN_INFORMATION,
T1_HOME_SCREEN_DEFAULT_IMAGES,
} from '@onekeyhq/shared/src/utils/deviceHomeScreenUtils';
import deviceUtils from '@onekeyhq/shared/src/utils/deviceUtils';
import timerUtils from '@onekeyhq/shared/src/utils/timerUtils';
import type {
Expand Down Expand Up @@ -789,11 +793,15 @@ class ServiceHardware extends ServiceBase {
await CoreSDKLoader();
const device = await localDb.getDevice(checkIsDefined(dbDeviceId));
let names = getHomeScreenDefaultList(device.featuresInfo || ({} as any));
if (['classic', 'mini', 'classic1s'].includes(device.deviceType)) {
// genesis.png is trezor brand image
names = names.filter((name) => name !== 'genesis');

const isT1Model = deviceHomeScreenUtils.isMonochromeScreen(
device.deviceType,
);
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved

if (isT1Model) {
names = T1_HOME_SCREEN_DEFAULT_IMAGES;
}
const size = getHomeScreenSize({
let size = getHomeScreenSize({
deviceType: device.deviceType,
homeScreenType,
thumbnail: false,
Expand All @@ -803,6 +811,9 @@ class ServiceHardware extends ServiceBase {
homeScreenType,
thumbnail: true,
});
if (!size && isT1Model) {
size = DEFAULT_T1_HOME_SCREEN_INFORMATION;
}
return { names, size, thumbnailSize };
}

Expand Down
20 changes: 20 additions & 0 deletions packages/kit-bg/src/webembeds/WebEmbedApiImageUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import imageUtils from '@onekeyhq/shared/src/utils/imageUtils';

class WebEmbedApiImageUtils {
async base64ImageToBitmap(params: {
base64: string;
width: number;
height: number;
}): Promise<string> {
return imageUtils.base64ImageToBitmap(params);
}

async convertToBlackAndWhiteImageBase64(
colorImageBase64: string,
mime: string,
): Promise<string> {
return imageUtils.convertToBlackAndWhiteImageBase64(colorImageBase64, mime);
}
}

export default WebEmbedApiImageUtils;
2 changes: 2 additions & 0 deletions packages/kit-bg/src/webembeds/instance/IWebembedApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type WebEmbedApiChainAdaLegacy from '../WebEmbedApiChainAdaLegacy';
import type WebEmbedApiImageUtils from '../WebEmbedApiImageUtils';
import type WebEmbedApiSecret from '../WebEmbedApiSecret';
import type WebEmbedApiTest from '../WebEmbedApiTest';

export type IWebembedApi = {
chainAdaLegacy: WebEmbedApiChainAdaLegacy;
test: WebEmbedApiTest;
imageUtils: WebEmbedApiImageUtils;
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
secret: WebEmbedApiSecret;
isSDKReady(): Promise<boolean>;
};
Expand Down
3 changes: 3 additions & 0 deletions packages/kit-bg/src/webembeds/instance/webembedApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const getOrCreateWebEmbedApiModule = memoizee(
if (name === 'test') {
return new (await import('../WebEmbedApiTest')).default();
}
if (name === 'imageUtils') {
return new (await import('../WebEmbedApiImageUtils')).default();
}
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
`Unknown WebEmbed API module: ${
name as string
Expand Down
4 changes: 4 additions & 0 deletions packages/kit-bg/src/webembeds/instance/webembedApiProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RemoteApiProxyBase } from '../../apis/RemoteApiProxyBase';
import type { IWebembedApi, IWebembedApiKeys } from './IWebembedApi';
import type { IBackgroundApiWebembedCallMessage } from '../../apis/IBackgroundApi';
import type WebEmbedApiChainAdaLegacy from '../WebEmbedApiChainAdaLegacy';
import type WebEmbedApiImageUtils from '../WebEmbedApiImageUtils';
import type WebEmbedApiSecret from '../WebEmbedApiSecret';
import type WebEmbedApiTest from '../WebEmbedApiTest';

Expand Down Expand Up @@ -69,6 +70,9 @@ class WebembedApiProxy extends RemoteApiProxyBase implements IWebembedApi {

secret: WebEmbedApiSecret =
this._createProxyModule<IWebembedApiKeys>('secret');

imageUtils: WebEmbedApiImageUtils =
this._createProxyModule<IWebembedApiKeys>('imageUtils');
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
}

const webembedApiProxy = new WebembedApiProxy();
Expand Down
4 changes: 3 additions & 1 deletion packages/kit/src/components/TabPageHeader/HeaderRight.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ReactNode } from 'react';
import { useCallback, useMemo } from 'react';

import { useIntl } from 'react-intl';
Expand Down Expand Up @@ -86,6 +87,7 @@ export function HeaderRight({
};
const layoutExtView = (
<ActionList
key="layoutExtView"
title={intl.formatMessage({
id: ETranslations.global_layout,
})}
Expand Down Expand Up @@ -144,7 +146,7 @@ export function HeaderRight({
onPress={onScanButtonPressed}
/>
);
let notificationsButton: React.ReactNode = (
let notificationsButton: ReactNode | null = (
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
<Stack key="notifications">
<HeaderIconButton
title={intl.formatMessage({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ import type {
EAccountManagerStacksRoutes,
IAccountManagerStacksParamList,
} from '@onekeyhq/shared/src/routes';
import deviceHomeScreenUtils from '@onekeyhq/shared/src/utils/deviceHomeScreenUtils';
import imageUtils from '@onekeyhq/shared/src/utils/imageUtils';
import { generateUUID } from '@onekeyhq/shared/src/utils/miscUtils';

import hardwareHomeScreenData from './hardwareHomeScreenData';
import uploadedHomeScreenCache from './uploadedHomeScreenCache';

import type { IHardwareHomeScreenData } from './hardwareHomeScreenData';
import type {
IHardwareHomeScreenData,
IHardwareHomeScreenName,
} from './hardwareHomeScreenData';
import type { IDeviceType } from '@onekeyfe/hd-core';
import type { DimensionValue } from 'react-native';

Expand Down Expand Up @@ -193,6 +197,7 @@ export default function HardwareHomeScreenModal({
let canUpload = false;
if (['classic', 'mini', 'classic1s'].includes(deviceType)) {
dataList = hardwareHomeScreenData.classicMini;
canUpload = true;
}
if (['touch'].includes(deviceType)) {
dataList = hardwareHomeScreenData.touch;
Expand Down Expand Up @@ -247,34 +252,43 @@ export default function HardwareHomeScreenModal({
const originW = data?.width;
const originH = data?.height;

const isMonochrome = deviceHomeScreenUtils.isMonochromeScreen(
device.deviceType,
);

const imgBase64: string = data.data;

const img = await imageUtils.resizeImage({
uri: data.data,
uri: imgBase64,

width: result?.config.size?.width,
height: result?.config.size?.height,

originW,
originH,
isMonochrome,
});
const imgThumb = await imageUtils.resizeImage({
uri: data.data,
uri: imgBase64,

width: result?.config.thumbnailSize?.width ?? result?.config.size?.width,
height:
result?.config.thumbnailSize?.height ?? result?.config.size?.height,

originW,
originH,
isMonochrome,
});

// setResizedImagePreview({
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
// base64Img: img?.uri,
// base64ThumbnailImg: imgThumb?.uri,
// });

const name = `${USER_UPLOAD_IMG_NAME_PREFIX}${generateUUID()}`;
const name =
`${USER_UPLOAD_IMG_NAME_PREFIX}${generateUUID()}` as IHardwareHomeScreenName;
const uploadItem: IHardwareHomeScreenData = {
uri: data.data,
uri: imageUtils.prefixBase64Uri(img?.base64 || imgBase64, 'image/jpeg'), // base64 data uri
hex: img?.hex,
thumbnailHex: imgThumb?.hex,
name,
Expand All @@ -283,7 +297,7 @@ export default function HardwareHomeScreenModal({
setUploadItems([...uploadItems, uploadItem]);
setSelectedItem(uploadItem);
uploadedHomeScreenCache.saveCache(device?.id, uploadItem);
}, [result?.config, uploadItems, device?.id]);
}, [result?.config, device.deviceType, device?.id, uploadItems]);

return (
<Page scrollEnabled safeAreaEnabled>
Expand Down Expand Up @@ -367,10 +381,33 @@ export default function HardwareHomeScreenModal({
return;
}
setIsLoading(true);

let customHex = '';
if (deviceHomeScreenUtils.isMonochromeScreen(device.deviceType)) {
const imgUri =
(await imageUtils.getBase64FromRequiredImageSource(
selectedItem?.source,
)) ||
selectedItem?.uri ||
'';
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
console.log(
'imgUri >>>>>>>>>>>>>>>>>++++++++>>> ',
imgUri,
selectedItem,
);
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
if (!imgUri) {
throw new Error('Error imgUri not defined');
sidmorizon marked this conversation as resolved.
Show resolved Hide resolved
}
customHex = await deviceHomeScreenUtils.imagePathToHex(
imgUri,
device.deviceType,
);
}

await backgroundApiProxy.serviceHardware.setDeviceHomeScreen({
dbDeviceId: device?.id,
imgName: selectedItem.name,
imgHex: selectedItem.hex || '',
imgHex: customHex || selectedItem.hex || '',
thumbnailHex: selectedItem.thumbnailHex || '',
isUserUpload: selectedItem.isUserUpload,
});
Expand Down
Loading
Loading