最近在Android R的一个项目上,Google 在基于Launcher3的基础上,集成了一个OnDeviceAppPrediction服务。功能呢,就是在All App View界面上端显示常用的应用和近期使用的应用。在所接触的项目中,经测试同事的辛苦挖掘,找出了不少问题:
1.Google对出现在近期列表中的应用执行卸载操作后做一个填充列表的动作,导致列表出现空缺。
2.使用其他应用后,没有实时改刷新界面显示。
一下便是我针对这两个问题的做的优化:
1.卸载应用后,做填充
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.apppredictionservice;
import android.app.prediction.AppPredictionContext;
import android.app.prediction.AppPredictionSessionId;
import android.app.prediction.AppTarget;
import android.app.prediction.AppTargetEvent;
import android.app.prediction.AppTargetId;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.CancellationSignal;
import android.service.appprediction.AppPredictionService;
import android.util.Log;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import static android.os.Process.myUserHandle;
import static android.text.TextUtils.isEmpty;
import static java.util.Collections.emptyList;
/*
* New plugin that replaces prediction driven Aiai APK in P
* PredictionService simply populates the top row of the app
* drawer with the 5 most recently used apps. Each time a new
* app is launched, it is added to the left of the top row.
* Duplicates are not added.
*/
public class PredictionService extends AppPredictionService {
public static final String MY_PREF = "mypref";
private static final String TAG = PredictionService.class.getSimpleName();
private static final int MAX_NUM = 5;
private static final String[] DEFAULT_PACKAGES = new String[]{
"com.google.android.apps.photos", // Photos
"com.google.android.apps.maps", // Maps
"com.google.android.gm", // Gmail
"com.android.settings", // Settings
"com.google.android.calculator" //Calculator
};
private final Set<AppPredictionSessionId> activeLauncherSessions = new HashSet<>();
private final List<AppTarget> predictionList = new ArrayList<>(5);
private final List<String> appNames = new ArrayList<>(5);
private final String[] appNameKeys = new String[] {
"first", "second", "third", "fourth", "fifth" };
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
private PackageRemovedBroadcastReceiver mPackageRemovedBroadcastReceiver;
private String mMostRecentComponent;
private AppTarget mAppTarget;
private String mPackageName;
private Context mContext;
private boolean mRemovedLast = true;
private boolean mDebug = false;
private boolean mAppSuggestionsEnabled = true;