1 /* 2 * Copyright (C) 2024 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 com.android.server.appsearch.appsindexer; 18 19 import android.annotation.NonNull; 20 import android.annotation.WorkerThread; 21 import android.app.appsearch.exceptions.AppSearchException; 22 import android.app.usage.UsageStatsManager; 23 import android.content.Context; 24 25 import com.android.server.appsearch.appsindexer.appsearchtypes.AppOpenEvent; 26 27 import java.io.Closeable; 28 import java.util.List; 29 import java.util.Objects; 30 31 /** 32 * Interacts with UsageStatsManager and AppSearch to index app open events. 33 * 34 * <p>This class is NOT thread-safe. 35 * 36 * @hide 37 */ 38 public final class AppOpenEventIndexerImpl implements Closeable { 39 static final String TAG = "AppSearchAppOpenEventIndexerImpl"; 40 41 private final Context mContext; 42 private final AppSearchHelper mAppSearchHelper; 43 AppOpenEventIndexerImpl(@onNull Context context)44 public AppOpenEventIndexerImpl(@NonNull Context context) throws AppSearchException { 45 mContext = Objects.requireNonNull(context); 46 mAppSearchHelper = new AppSearchHelper(context); 47 } 48 49 /** 50 * Checks UsageStatsManager and AppSearch to sync the App Open Events Index in AppSearch. 51 * 52 * @param settings contains update timestamps that help the indexer determine when indexing last 53 * ran 54 */ 55 @WorkerThread doUpdate(@onNull AppOpenEventIndexerSettings settings)56 public void doUpdate(@NonNull AppOpenEventIndexerSettings settings) throws AppSearchException { 57 Objects.requireNonNull(settings); 58 59 UsageStatsManager usageStatsManager = mContext.getSystemService(UsageStatsManager.class); 60 61 long currentTimeMillis = System.currentTimeMillis(); 62 long lastAppOpenIndexerUpdateTimeMillis = settings.getLastUpdateTimestampMillis(); 63 List<AppOpenEvent> appOpenEvents = 64 AppsUtil.getAppOpenEvents( 65 usageStatsManager, lastAppOpenIndexerUpdateTimeMillis, currentTimeMillis); 66 67 try { 68 // This should be a no-op if the schema is already set and unchanged. 69 mAppSearchHelper.setSchemaForAppOpenEvents(); 70 71 mAppSearchHelper.indexAppOpenEvents(appOpenEvents); 72 settings.setLastUpdateTimestampMillis(currentTimeMillis); 73 } catch (AppSearchException e) { 74 // Reset the last update time stamp and app update timestamp so we can try again later. 75 settings.reset(); 76 throw e; 77 } 78 } 79 80 /** Shuts down the {@link AppsOpenEventIndexerImpl} and its {@link AppSearchHelper}. */ 81 @Override close()82 public void close() { 83 mAppSearchHelper.close(); 84 } 85 } 86