Merge pull request #2878 from Goooler/OverlayCallbackImpl

Convert OverlayCallbackImpl to Kotlin
This commit is contained in:
Yasan
2022-08-28 12:46:48 +04:30
committed by GitHub
2 changed files with 143 additions and 177 deletions
@@ -1,177 +0,0 @@
package app.lawnchair.nexuslauncher;
import static android.content.pm.ApplicationInfo.FLAG_DEBUGGABLE;
import static android.content.pm.ApplicationInfo.FLAG_SYSTEM;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import com.android.launcher3.Launcher;
import com.android.launcher3.Utilities;
import com.android.systemui.plugins.shared.LauncherOverlayManager;
import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay;
import com.google.android.libraries.launcherclient.ISerializableScrollCallback;
import com.google.android.libraries.launcherclient.LauncherClient;
import com.google.android.libraries.launcherclient.LauncherClientCallbacks;
import com.google.android.libraries.launcherclient.StaticInteger;
import com.patrykmichalik.opto.core.PreferenceExtensionsKt;
import app.lawnchair.FeedBridge;
import app.lawnchair.LawnchairLauncher;
import app.lawnchair.preferences2.PreferenceManager2;
/**
* Implements {@link LauncherOverlay} and passes all the corresponding events to {@link
* LauncherClient}. {@see setClient}
*
* <p>Implements {@link LauncherClientCallbacks} and sends all the corresponding callbacks to {@link
* Launcher}.
*/
public class OverlayCallbackImpl
implements LauncherOverlay, LauncherClientCallbacks, LauncherOverlayManager,
ISerializableScrollCallback {
final static String PREF_PERSIST_FLAGS = "pref_persistent_flags";
private final Launcher mLauncher;
private final LauncherClient mClient;
boolean mFlagsChanged = false;
private LauncherOverlayCallbacks mLauncherOverlayCallbacks;
private boolean mWasOverlayAttached = false;
private int mFlags;
public OverlayCallbackImpl(LawnchairLauncher launcher) {
PreferenceManager2 preferenceManager2 = PreferenceManager2.getInstance(launcher);
Boolean enableFeed = PreferenceExtensionsKt.firstBlocking(preferenceManager2.getEnableFeed());
mLauncher = launcher;
mClient = new LauncherClient(mLauncher, this, new StaticInteger(
(enableFeed ? 1 : 0) | 2 | 4 | 8));
}
public static boolean minusOneAvailable(Context context) {
return FeedBridge.useBridge(context)
|| ((context.getApplicationInfo().flags & (FLAG_DEBUGGABLE | FLAG_SYSTEM)) != 0);
}
@Override
public void onDeviceProvideChanged() {
mClient.redraw();
}
@Override
public void onAttachedToWindow() {
mClient.onAttachedToWindow();
}
@Override
public void onDetachedFromWindow() {
mClient.onDetachedFromWindow();
}
@Override
public void openOverlay() {
mClient.showOverlay(true);
}
@Override
public void hideOverlay(boolean animate) {
mClient.hideOverlay(animate);
}
@Override
public void hideOverlay(int duration) {
mClient.hideOverlay(duration);
}
@Override
public boolean startSearch(byte[] config, Bundle extras) {
return false;
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
// Not called
}
@Override
public void onActivityStarted(Activity activity) {
mClient.onStart();
}
@Override
public void onActivityResumed(Activity activity) {
mClient.onResume();
}
@Override
public void onActivityPaused(Activity activity) {
mClient.onPause();
}
@Override
public void onActivityStopped(Activity activity) {
mClient.onStop();
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
mClient.onDestroy();
mClient.mDestroyed = true;
}
@Override
public void onOverlayScrollChanged(float progress) {
if (mLauncherOverlayCallbacks != null) {
mLauncherOverlayCallbacks.onScrollChanged(progress);
}
}
@Override
public void onServiceStateChanged(boolean overlayAttached, boolean hotwordActive) {
this.onServiceStateChanged(overlayAttached);
}
@Override
public void onServiceStateChanged(boolean overlayAttached) {
if (overlayAttached != mWasOverlayAttached) {
mWasOverlayAttached = overlayAttached;
mLauncher.setLauncherOverlay(overlayAttached ? this : null);
}
}
@Override
public void onScrollInteractionBegin() {
mClient.startScroll();
}
@Override
public void onScrollInteractionEnd() {
mClient.endScroll();
}
@Override
public void onScrollChange(float progress, boolean rtl) {
mClient.setScroll(progress);
}
@Override
public void setOverlayCallbacks(LauncherOverlayCallbacks callbacks) {
mLauncherOverlayCallbacks = callbacks;
}
@Override
public void setPersistentFlags(int flags) {
flags &= (8 | 16);
if (flags != mFlags) {
mFlagsChanged = true;
mFlags = flags;
Utilities.getDevicePrefs(mLauncher).edit().putInt(PREF_PERSIST_FLAGS, flags).apply();
}
}
}
@@ -0,0 +1,143 @@
package app.lawnchair.nexuslauncher
import android.app.Activity
import android.content.Context
import android.content.pm.ApplicationInfo
import android.os.Bundle
import app.lawnchair.FeedBridge
import app.lawnchair.LawnchairLauncher
import app.lawnchair.preferences2.PreferenceManager2
import com.android.launcher3.Launcher
import com.android.launcher3.Utilities
import com.android.systemui.plugins.shared.LauncherOverlayManager
import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlay
import com.android.systemui.plugins.shared.LauncherOverlayManager.LauncherOverlayCallbacks
import com.google.android.libraries.launcherclient.ISerializableScrollCallback
import com.google.android.libraries.launcherclient.LauncherClient
import com.google.android.libraries.launcherclient.LauncherClientCallbacks
import com.google.android.libraries.launcherclient.LauncherClientService
import com.google.android.libraries.launcherclient.StaticInteger
import com.patrykmichalik.opto.core.firstBlocking
/**
* Implements [LauncherOverlay] and passes all the corresponding events to [LauncherClient],
* see [LauncherClientService.setClient].
*
* Implements [LauncherClientCallbacks] and sends all the corresponding callbacks to [Launcher].
*/
class OverlayCallbackImpl(private val mLauncher: LawnchairLauncher) : LauncherOverlay,
LauncherClientCallbacks, LauncherOverlayManager, ISerializableScrollCallback {
private val mClient: LauncherClient
private var mFlagsChanged = false
private var mLauncherOverlayCallbacks: LauncherOverlayCallbacks? = null
private var mWasOverlayAttached = false
private var mFlags = 0
init {
val enableFeed = PreferenceManager2.getInstance(mLauncher).enableFeed.firstBlocking()
mClient = LauncherClient(
mLauncher, this, StaticInteger((if (enableFeed) 1 else 0) or 2 or 4 or 8)
)
}
override fun onDeviceProvideChanged() {
mClient.redraw()
}
override fun onAttachedToWindow() {
mClient.onAttachedToWindow()
}
override fun onDetachedFromWindow() {
mClient.onDetachedFromWindow()
}
override fun openOverlay() {
mClient.showOverlay(true)
}
override fun hideOverlay(animate: Boolean) {
mClient.hideOverlay(animate)
}
override fun hideOverlay(duration: Int) {
mClient.hideOverlay(duration)
}
override fun startSearch(config: ByteArray?, extras: Bundle?): Boolean = false
override fun onActivityCreated(activity: Activity, bundle: Bundle?) = Unit
override fun onActivityStarted(activity: Activity) {
mClient.onStart()
}
override fun onActivityResumed(activity: Activity) {
mClient.onResume()
}
override fun onActivityPaused(activity: Activity) {
mClient.onPause()
}
override fun onActivityStopped(activity: Activity) {
mClient.onStop()
}
override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) = Unit
override fun onActivityDestroyed(activity: Activity) {
mClient.onDestroy()
mClient.mDestroyed = true
}
override fun onOverlayScrollChanged(progress: Float) {
mLauncherOverlayCallbacks?.onScrollChanged(progress)
}
override fun onServiceStateChanged(overlayAttached: Boolean, hotwordActive: Boolean) {
onServiceStateChanged(overlayAttached)
}
override fun onServiceStateChanged(overlayAttached: Boolean) {
if (overlayAttached != mWasOverlayAttached) {
mWasOverlayAttached = overlayAttached
mLauncher.setLauncherOverlay(if (overlayAttached) this else null)
}
}
override fun onScrollInteractionBegin() {
mClient.startScroll()
}
override fun onScrollInteractionEnd() {
mClient.endScroll()
}
override fun onScrollChange(progress: Float, rtl: Boolean) {
mClient.setScroll(progress)
}
override fun setOverlayCallbacks(callbacks: LauncherOverlayCallbacks?) {
mLauncherOverlayCallbacks = callbacks
}
override fun setPersistentFlags(flags: Int) {
val newFlags = flags and (8 or 16)
if (newFlags != mFlags) {
mFlagsChanged = true
mFlags = newFlags
Utilities.getDevicePrefs(mLauncher).edit().putInt(PREF_PERSIST_FLAGS, newFlags).apply()
}
}
companion object {
private const val PREF_PERSIST_FLAGS = "pref_persistent_flags"
fun minusOneAvailable(context: Context): Boolean {
return FeedBridge.useBridge(context) ||
context.applicationInfo.flags and
(ApplicationInfo.FLAG_DEBUGGABLE or ApplicationInfo.FLAG_SYSTEM) != 0
}
}
}