2018-06-29 14:40:18 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 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.launcher3.util;
|
|
|
|
|
|
2019-08-15 14:53:41 -07:00
|
|
|
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
|
|
|
|
|
2018-06-29 14:40:18 -07:00
|
|
|
import android.content.Context;
|
|
|
|
|
import android.os.Looper;
|
2021-08-27 21:22:17 +00:00
|
|
|
import android.util.Log;
|
2018-06-29 14:40:18 -07:00
|
|
|
|
2021-08-27 21:22:17 +00:00
|
|
|
import androidx.annotation.UiThread;
|
2019-08-06 09:48:36 -07:00
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
|
|
2024-09-13 15:03:11 -07:00
|
|
|
import com.android.launcher3.LauncherApplication;
|
2019-08-06 09:48:36 -07:00
|
|
|
import com.android.launcher3.util.ResourceBasedOverride.Overrides;
|
2018-06-29 14:40:18 -07:00
|
|
|
|
2021-08-27 21:22:17 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2018-06-29 14:40:18 -07:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2024-04-21 00:13:35 -07:00
|
|
|
import java.util.function.Consumer;
|
2018-06-29 14:40:18 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Utility class for defining singletons which are initiated on main thread.
|
2024-09-10 17:29:37 -07:00
|
|
|
*
|
|
|
|
|
* TODO(b/361850561): Do not delete MainThreadInitializedObject until we find a way to
|
|
|
|
|
* unregister and understand how singleton objects are destroyed in dagger graph.
|
2018-06-29 14:40:18 -07:00
|
|
|
*/
|
2024-04-21 00:13:35 -07:00
|
|
|
public class MainThreadInitializedObject<T extends SafeCloseable> {
|
2018-06-29 14:40:18 -07:00
|
|
|
|
|
|
|
|
private final ObjectProvider<T> mProvider;
|
|
|
|
|
private T mValue;
|
|
|
|
|
|
|
|
|
|
public MainThreadInitializedObject(ObjectProvider<T> provider) {
|
|
|
|
|
mProvider = provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T get(Context context) {
|
2024-04-21 00:13:35 -07:00
|
|
|
Context app = context.getApplicationContext();
|
|
|
|
|
if (app instanceof SandboxApplication sc) {
|
2023-05-01 16:55:59 -07:00
|
|
|
return sc.getObject(this);
|
Render user's actual workspace in ThemePicker preview (Part 3)
go/grid-migration-preview
With this change, we can see actual grid migration in wallpaper preview.
The approach here: we use a tmp table (favorites_preview) here specifically for this preview (to write off the migration results), and load from this tmp table workspace items if migration is necessary and successful. Otherwise, we load from the current workspace.
UPDATED: this change should be completely compatible with the new multi-db grid migration algorithm. Here is why
1. In LauncherPreviewRender#renderScreenShot, I added a check to decide which grid migration preview method we should call. Once v2 preview method is implemented, it should be integrated with other parts of this change perfectly (the reason will be mentioned below).
2. While we have multiple DBs, mOpenHelper in LauncherProvider always points to the current db we are using. Queries using CONTENT_URI is routed to whatever DB mOpenHelper points to, so it works perfectly to directly operate on CONTENT_URI even when we use multi-db underneath the hood.
3. With 1 and 2 mentioned, I believe in order for this preview change to support multi-db, we only need to implement the V2 grid migration algorithm. Because most of what we are doing in this change is wrapped in GridSizeMigrationTask, it's perfectly safeguarded.
Bug: 144052839
Change-Id: Ie6d6048d77326f96546c8a180a7cd8f15b47e4c4
2020-01-12 01:07:59 -08:00
|
|
|
}
|
|
|
|
|
|
2018-06-29 14:40:18 -07:00
|
|
|
if (mValue == null) {
|
|
|
|
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
2024-04-21 00:13:35 -07:00
|
|
|
mValue = TraceHelper.allowIpcs("main.thread.object", () -> mProvider.get(app));
|
2018-06-29 14:40:18 -07:00
|
|
|
} else {
|
|
|
|
|
try {
|
2019-08-15 14:53:41 -07:00
|
|
|
return MAIN_EXECUTOR.submit(() -> get(context)).get();
|
2018-06-29 14:40:18 -07:00
|
|
|
} catch (InterruptedException|ExecutionException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mValue;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 00:13:35 -07:00
|
|
|
/**
|
|
|
|
|
* Executes the callback is the value is already created
|
|
|
|
|
* @return true if the callback was executed, false otherwise
|
|
|
|
|
*/
|
|
|
|
|
public boolean executeIfCreated(Consumer<T> callback) {
|
|
|
|
|
T v = mValue;
|
|
|
|
|
if (v != null) {
|
|
|
|
|
callback.accept(v);
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-06-29 14:40:18 -07:00
|
|
|
}
|
|
|
|
|
|
2019-04-19 01:46:51 -07:00
|
|
|
@VisibleForTesting
|
|
|
|
|
public void initializeForTesting(T value) {
|
|
|
|
|
mValue = value;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-06 09:48:36 -07:00
|
|
|
/**
|
|
|
|
|
* Initializes a provider based on resource overrides
|
|
|
|
|
*/
|
2024-04-21 00:13:35 -07:00
|
|
|
public static <T extends ResourceBasedOverride & SafeCloseable> MainThreadInitializedObject<T>
|
|
|
|
|
forOverride(Class<T> clazz, int resourceId) {
|
2019-08-06 09:48:36 -07:00
|
|
|
return new MainThreadInitializedObject<>(c -> Overrides.getObject(clazz, c, resourceId));
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 14:40:18 -07:00
|
|
|
public interface ObjectProvider<T> {
|
|
|
|
|
|
|
|
|
|
T get(Context context);
|
|
|
|
|
}
|
2021-08-27 21:22:17 +00:00
|
|
|
|
2024-04-21 00:13:35 -07:00
|
|
|
public interface SandboxApplication {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Find a cached object from mObjectMap if we have already created one. If not, generate
|
|
|
|
|
* an object using the provider.
|
|
|
|
|
*/
|
|
|
|
|
<T extends SafeCloseable> T getObject(MainThreadInitializedObject<T> object);
|
|
|
|
|
|
2024-09-25 10:49:47 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Put a value into cache, can be used to put mocked MainThreadInitializedObject
|
|
|
|
|
* instances.
|
|
|
|
|
*/
|
|
|
|
|
<T extends SafeCloseable> void putObject(MainThreadInitializedObject<T> object, T value);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether this context should cleanup all objects when its destroyed or leave it
|
|
|
|
|
* to the GC.
|
|
|
|
|
* These objects can have listeners attached to the system server and mey not be able to get
|
|
|
|
|
* GCed themselves when running on a device.
|
|
|
|
|
* Some environments like Robolectric tear down the whole system at the end of the test,
|
|
|
|
|
* so manual cleanup may not be required.
|
|
|
|
|
*/
|
|
|
|
|
default boolean shouldCleanUpOnDestroy() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 00:13:35 -07:00
|
|
|
@UiThread
|
|
|
|
|
default <T extends SafeCloseable> T createObject(MainThreadInitializedObject<T> object) {
|
|
|
|
|
return object.mProvider.get((Context) this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 21:22:17 +00:00
|
|
|
/**
|
|
|
|
|
* Abstract Context which allows custom implementations for
|
|
|
|
|
* {@link MainThreadInitializedObject} providers
|
|
|
|
|
*/
|
2024-09-13 15:03:11 -07:00
|
|
|
public static class SandboxContext extends LauncherApplication implements SandboxApplication {
|
2021-08-27 21:22:17 +00:00
|
|
|
|
|
|
|
|
private static final String TAG = "SandboxContext";
|
|
|
|
|
|
2024-04-21 00:13:35 -07:00
|
|
|
private final Map<MainThreadInitializedObject, Object> mObjectMap = new HashMap<>();
|
|
|
|
|
private final ArrayList<SafeCloseable> mOrderedObjects = new ArrayList<>();
|
2021-08-27 21:22:17 +00:00
|
|
|
|
|
|
|
|
private final Object mDestroyLock = new Object();
|
|
|
|
|
private boolean mDestroyed = false;
|
|
|
|
|
|
2024-04-21 00:13:35 -07:00
|
|
|
public SandboxContext(Context base) {
|
2024-09-13 15:03:11 -07:00
|
|
|
attachBaseContext(base);
|
|
|
|
|
initDagger();
|
2021-08-27 21:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Context getApplicationContext() {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:49:47 -07:00
|
|
|
@Override
|
|
|
|
|
public boolean shouldCleanUpOnDestroy() {
|
|
|
|
|
return (getBaseContext().getApplicationContext() instanceof SandboxApplication sa)
|
|
|
|
|
? sa.shouldCleanUpOnDestroy() : true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 21:22:17 +00:00
|
|
|
public void onDestroy() {
|
2024-09-25 10:49:47 -07:00
|
|
|
if (shouldCleanUpOnDestroy()) {
|
|
|
|
|
cleanUpObjects();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void cleanUpObjects() {
|
2024-09-13 15:03:11 -07:00
|
|
|
getAppComponent().getDaggerSingletonTracker().close();
|
2021-08-27 21:22:17 +00:00
|
|
|
synchronized (mDestroyLock) {
|
|
|
|
|
// Destroy in reverse order
|
|
|
|
|
for (int i = mOrderedObjects.size() - 1; i >= 0; i--) {
|
2024-04-21 00:13:35 -07:00
|
|
|
mOrderedObjects.get(i).close();
|
2021-08-27 21:22:17 +00:00
|
|
|
}
|
|
|
|
|
mDestroyed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-21 00:13:35 -07:00
|
|
|
@Override
|
|
|
|
|
public <T extends SafeCloseable> T getObject(MainThreadInitializedObject<T> object) {
|
2021-08-27 21:22:17 +00:00
|
|
|
synchronized (mDestroyLock) {
|
|
|
|
|
if (mDestroyed) {
|
|
|
|
|
Log.e(TAG, "Static object access with a destroyed context");
|
|
|
|
|
}
|
|
|
|
|
T t = (T) mObjectMap.get(object);
|
|
|
|
|
if (t != null) {
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
if (Looper.myLooper() == Looper.getMainLooper()) {
|
2023-05-01 16:55:59 -07:00
|
|
|
t = createObject(object);
|
2021-08-27 21:22:17 +00:00
|
|
|
mObjectMap.put(object, t);
|
|
|
|
|
mOrderedObjects.add(t);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2023-05-01 16:55:59 -07:00
|
|
|
return MAIN_EXECUTOR.submit(() -> getObject(object)).get();
|
2021-08-27 21:22:17 +00:00
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-25 10:49:47 -07:00
|
|
|
@Override
|
2024-04-21 00:13:35 -07:00
|
|
|
public <T extends SafeCloseable> void putObject(
|
|
|
|
|
MainThreadInitializedObject<T> object, T value) {
|
2023-10-12 11:29:14 +01:00
|
|
|
mObjectMap.put(object, value);
|
|
|
|
|
}
|
2021-08-27 21:22:17 +00:00
|
|
|
}
|
2018-06-29 14:40:18 -07:00
|
|
|
}
|