1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.ondevicepersonalization; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.ServiceConnection; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.ondevicepersonalization.aidl.IOnDevicePersonalizationManagerService; 28 import android.os.IBinder; 29 import android.util.Slog; 30 31 import java.util.List; 32 33 /** 34 * OnDevicePersonalization Manager. 35 * 36 * @hide 37 */ 38 public class OnDevicePersonalizationManager { 39 public static final String ON_DEVICE_PERSONALIZATION_SERVICE = 40 "on_device_personalization_service"; 41 42 private boolean mBound = false; 43 private static final String TAG = "OdpManager"; 44 45 private IOnDevicePersonalizationManagerService mService; 46 private final Context mContext; 47 OnDevicePersonalizationManager(Context context)48 public OnDevicePersonalizationManager(Context context) { 49 mContext = context; 50 } 51 52 private final ServiceConnection mConnection = new ServiceConnection() { 53 @Override 54 public void onServiceConnected(ComponentName name, IBinder service) { 55 mService = IOnDevicePersonalizationManagerService.Stub.asInterface(service); 56 mBound = true; 57 } 58 59 @Override 60 public void onServiceDisconnected(ComponentName name) { 61 mService = null; 62 mBound = false; 63 } 64 }; 65 66 private static final int BIND_SERVICE_INTERVAL_MS = 1000; 67 private static final int BIND_SERVICE_RETRY_TIMES = 3; 68 private static final String VERSION = "1.0"; 69 70 /** 71 * Gets OnDevicePersonalization version. 72 * 73 * @hide 74 */ getVersion()75 public String getVersion() { 76 return VERSION; 77 } 78 /** 79 * Find the ComponentName of the service, given its intent and package manager. 80 * 81 * @return ComponentName of the service. Null if the service is not found. 82 */ resolveService(@onNull Intent intent, @NonNull PackageManager pm)83 private @Nullable ComponentName resolveService(@NonNull Intent intent, 84 @NonNull PackageManager pm) { 85 List<ResolveInfo> services = pm.queryIntentServices(intent, 86 PackageManager.ResolveInfoFlags.of(0)); 87 if (services == null || services.isEmpty()) { 88 Slog.e(TAG, "Failed to find ondevicepersonalization service"); 89 return null; 90 } 91 92 for (int i = 0; i < services.size(); i++) { 93 ResolveInfo ri = services.get(i); 94 ComponentName resolved = new ComponentName( 95 ri.serviceInfo.packageName, ri.serviceInfo.name); 96 // There should only be one matching service inside the given package. 97 // If there's more than one, return the first one found. 98 return resolved; 99 } 100 Slog.e(TAG, "Didn't find any matching ondevicepersonalization service."); 101 return null; 102 } 103 } 104