Skip to content

Commit

Permalink
fix some lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesa2 committed Mar 23, 2020
1 parent d38e307 commit 27b28e3
Showing 1 changed file with 22 additions and 27 deletions.
Expand Up @@ -30,62 +30,57 @@ import androidx.slice.core.SliceHints
import androidx.slice.widget.SliceLiveData
import androidx.slice.widget.SliceView
import androidx.slice.widget.SliceView.OnSliceActionListener
import com.example.android.sliceviewer.ui.list.SliceViewerActivity
import com.example.android.sliceviewer.ui.list.SliceViewerActivity.Companion.TAG

fun SliceView.bind(
context: Context,
lifecycleOwner: LifecycleOwner,
uri: Uri,
onSliceActionListener: OnSliceActionListener = OnSliceActionListener { _, _ -> },
onClickListener: OnClickListener = OnClickListener { },
onLongClickListener: OnLongClickListener = OnLongClickListener { false },
scrollable: Boolean = false
context: Context,
lifecycleOwner: LifecycleOwner,
uri: Uri,
onSliceActionListener: OnSliceActionListener = OnSliceActionListener { _, _ -> },
onClickListener: OnClickListener = OnClickListener { },
onLongClickListener: OnLongClickListener = OnLongClickListener { false },
scrollable: Boolean = false
) {
setOnSliceActionListener(onSliceActionListener)
setOnClickListener(onClickListener)
setScrollable(scrollable)
isScrollable = scrollable
setOnLongClickListener(onLongClickListener)
if (uri.scheme == null) {
Log.w(TAG, "Scheme is null for URI $uri")
return
}
// If someone accidentally prepends the "slice-" prefix to their scheme, let's remove it.
val scheme =
if (uri.scheme.startsWith("slice-")) {
uri.scheme.replace("slice-", "")
}
else {
uri.scheme
}
if (uri.scheme.startsWith("slice-")) {
uri.scheme.replace("slice-", "")
} else {
uri.scheme
}
if (scheme == ContentResolver.SCHEME_CONTENT ||
scheme.equals("https", true) ||
scheme.equals("http", true)
scheme.equals("https", true) ||
scheme.equals("http", true)
) {
val intent = Intent(Intent.ACTION_VIEW, uri)
val sliceLiveData = SliceLiveData.fromIntent(context, intent)
sliceLiveData?.removeObservers(lifecycleOwner)
sliceLiveData.removeObservers(lifecycleOwner)
try {
sliceLiveData?.observe(lifecycleOwner, Observer { updatedSlice ->
sliceLiveData.observe(lifecycleOwner, Observer { updatedSlice ->
if (updatedSlice == null) return@Observer
slice = updatedSlice
val expiry = SliceMetadata.from(context, updatedSlice).expiry
if (expiry != SliceHints.INFINITY) {
// Shows the updated text after the TTL expires.
postDelayed(
{ slice = updatedSlice },
expiry - System.currentTimeMillis() + 15
{ slice = updatedSlice },
expiry - System.currentTimeMillis() + 15
)
}
Log.d(SliceViewerActivity.TAG, "Update Slice: $updatedSlice")
Log.d(TAG, "Update Slice: $updatedSlice")
})
} catch (e: Exception) {
Log.e(
SliceViewerActivity.TAG,
"Failed to find a valid ContentProvider for authority: $uri"
)
Log.e(TAG, "Failed to find a valid ContentProvider for authority: $uri")
}
} else {
Log.w(SliceViewerActivity.TAG, "Invalid uri, skipping slice: $uri")
Log.w(TAG, "Invalid uri, skipping slice: $uri")
}
}

0 comments on commit 27b28e3

Please sign in to comment.