diff options
Diffstat (limited to 'packages/SystemUI/src/com/android/keyguard/KeyguardSliceViewController.java')
-rw-r--r-- | packages/SystemUI/src/com/android/keyguard/KeyguardSliceViewController.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/packages/SystemUI/src/com/android/keyguard/KeyguardSliceViewController.java b/packages/SystemUI/src/com/android/keyguard/KeyguardSliceViewController.java new file mode 100644 index 000000000000..68fa0b010a3e --- /dev/null +++ b/packages/SystemUI/src/com/android/keyguard/KeyguardSliceViewController.java @@ -0,0 +1,184 @@ +/* + * Copyright (C) 2020 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.keyguard; + +import static android.view.Display.DEFAULT_DISPLAY; + +import android.net.Uri; +import android.os.Trace; +import android.provider.Settings; +import android.util.Log; +import android.view.Display; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.Observer; +import androidx.slice.Slice; +import androidx.slice.SliceViewManager; +import androidx.slice.widget.SliceLiveData; + +import com.android.keyguard.dagger.KeyguardStatusViewScope; +import com.android.systemui.Dumpable; +import com.android.systemui.dump.DumpManager; +import com.android.systemui.keyguard.KeyguardSliceProvider; +import com.android.systemui.plugins.ActivityStarter; +import com.android.systemui.statusbar.policy.ConfigurationController; +import com.android.systemui.tuner.TunerService; + +import java.io.FileDescriptor; +import java.io.PrintWriter; + +import javax.inject.Inject; + +/** Controller for a {@link KeyguardSliceView}. */ +@KeyguardStatusViewScope +public class KeyguardSliceViewController implements Dumpable { + private static final String TAG = "KeyguardSliceViewController"; + + private final KeyguardSliceView mView; + private final ActivityStarter mActivityStarter; + private final ConfigurationController mConfigurationController; + private final TunerService mTunerService; + private final DumpManager mDumpManager; + private int mDisplayId; + private LiveData<Slice> mLiveData; + private Uri mKeyguardSliceUri; + private Slice mSlice; + + private final View.OnAttachStateChangeListener mOnAttachStateChangeListener = + new View.OnAttachStateChangeListener() { + + @Override + public void onViewAttachedToWindow(View v) { + + Display display = mView.getDisplay(); + if (display != null) { + mDisplayId = display.getDisplayId(); + } + mTunerService.addTunable(mTunable, Settings.Secure.KEYGUARD_SLICE_URI); + // Make sure we always have the most current slice + if (mDisplayId == DEFAULT_DISPLAY && mLiveData != null) { + mLiveData.observeForever(mObserver); + } + mConfigurationController.addCallback(mConfigurationListener); + mDumpManager.registerDumpable(TAG, KeyguardSliceViewController.this); + } + + @Override + public void onViewDetachedFromWindow(View v) { + + // TODO(b/117344873) Remove below work around after this issue be fixed. + if (mDisplayId == DEFAULT_DISPLAY) { + mLiveData.removeObserver(mObserver); + } + mTunerService.removeTunable(mTunable); + mConfigurationController.removeCallback(mConfigurationListener); + mDumpManager.unregisterDumpable(TAG); + } + }; + + TunerService.Tunable mTunable = (key, newValue) -> setupUri(newValue); + + ConfigurationController.ConfigurationListener mConfigurationListener = + new ConfigurationController.ConfigurationListener() { + @Override + public void onDensityOrFontScaleChanged() { + mView.onDensityOrFontScaleChanged(); + } + }; + + Observer<Slice> mObserver = new Observer<Slice>() { + @Override + public void onChanged(Slice slice) { + mSlice = slice; + mView.showSlice(slice); + } + }; + + @Inject + public KeyguardSliceViewController(KeyguardSliceView keyguardSliceView, + ActivityStarter activityStarter, ConfigurationController configurationController, + TunerService tunerService, DumpManager dumpManager) { + mView = keyguardSliceView; + mActivityStarter = activityStarter; + mConfigurationController = configurationController; + mTunerService = tunerService; + mDumpManager = dumpManager; + } + + /** Initialize the controller. */ + public void init() { + if (mView.isAttachedToWindow()) { + mOnAttachStateChangeListener.onViewAttachedToWindow(mView); + } + mView.addOnAttachStateChangeListener(mOnAttachStateChangeListener); + mView.setActivityStarter(mActivityStarter); + mView.setController(this); // TODO: remove this. + } + + /** + * Sets the slice provider Uri. + */ + public void setupUri(String uriString) { + if (uriString == null) { + uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI; + } + + boolean wasObserving = false; + if (mLiveData != null && mLiveData.hasActiveObservers()) { + wasObserving = true; + mLiveData.removeObserver(mObserver); + } + + mKeyguardSliceUri = Uri.parse(uriString); + mLiveData = SliceLiveData.fromUri(mView.getContext(), mKeyguardSliceUri); + + if (wasObserving) { + mLiveData.observeForever(mObserver); + } + } + + /** + * Update contents of the view. + */ + public void refresh() { + Slice slice; + Trace.beginSection("KeyguardSliceViewController#refresh"); + // We can optimize performance and avoid binder calls when we know that we're bound + // to a Slice on the same process. + if (KeyguardSliceProvider.KEYGUARD_SLICE_URI.equals(mKeyguardSliceUri.toString())) { + KeyguardSliceProvider instance = KeyguardSliceProvider.getAttachedInstance(); + if (instance != null) { + slice = instance.onBindSlice(mKeyguardSliceUri); + } else { + Log.w(TAG, "Keyguard slice not bound yet?"); + slice = null; + } + } else { + // TODO: Make SliceViewManager injectable + slice = SliceViewManager.getInstance(mView.getContext()).bindSlice(mKeyguardSliceUri); + } + mObserver.onChanged(slice); + Trace.endSection(); + } + + @Override + public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) { + pw.println(" mSlice: " + mSlice); + } +} |