Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly pin the slice to avoid a race condition. #204

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.View.OnLongClickListener
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.slice.SliceMetadata
import androidx.slice.SliceViewManager
import androidx.slice.core.SliceHints
import androidx.slice.widget.SliceLiveData
import androidx.slice.widget.SliceView
Expand Down Expand Up @@ -62,9 +63,17 @@ fun SliceView.bind(
scheme.equals("https", true) ||
scheme.equals("http", true)
) {
val intent = Intent(Intent.ACTION_VIEW, uri)
val sliceLiveData = SliceLiveData.fromIntent(context, intent)
val sliceLiveData = SliceLiveData.fromUri(context, uri)
sliceLiveData?.removeObservers(lifecycleOwner)
// There's a race condition between binding and pinning the slice if we
// don't pin it explicitly.
try {
SliceViewManager.getInstance(context).pinSlice(uri);
} catch (e: SecurityException) {
// We can only pin the slice if the viewer app already has been
// granted permission.
Log.d(SliceViewerActivity.TAG, "No permission yet.");
}
try {
sliceLiveData?.observe(lifecycleOwner, Observer { updatedSlice ->
if (updatedSlice == null) return@Observer
Expand All @@ -88,4 +97,4 @@ fun SliceView.bind(
} else {
Log.w(SliceViewerActivity.TAG, "Invalid uri, skipping slice: $uri")
}
}
}