Skip to content

Commit

Permalink
configure a shortcut to launch an activity using root shell #56
Browse files Browse the repository at this point in the history
  • Loading branch information
sdex committed Sep 18, 2023
1 parent 16ac1bc commit 0514f3b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ fun Activity.launchActivity(

fun Activity.launchActivity(
componentName: ComponentName,
isExported: Boolean,
useRoot: Boolean,
) {
if (isExported) {
if (useRoot) {
launchActivityWithRoot(this, componentName)
} else {
IntentUtils.launchActivity(
this,
componentName,
componentName.className.split(".").last(),
false
)
} else {
launchActivityWithRoot(this, componentName)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.drawable.toBitmap
import androidx.core.view.isVisible
import androidx.core.widget.doOnTextChanged
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
Expand Down Expand Up @@ -99,6 +100,8 @@ class AddShortcutDialogActivity : AppCompatActivity(), IconDialog.Callback {
override fun onLoadCleared(placeholder: Drawable?) {
}
})
binding.useRoot.isVisible = true
binding.useRoot.isChecked = !activityModel.exported
}

if (historyModel != null) {
Expand All @@ -124,11 +127,19 @@ class AddShortcutDialogActivity : AppCompatActivity(), IconDialog.Callback {

activityModel?.let {
val model = it.copy(name = shortcutName)
IntentUtils.createLauncherIcon(this, model, bitmap)
IntentUtils.createLauncherIcon(
this,
model,
bitmap,
binding.useRoot.isChecked
)
}

historyModel?.let {
createHistoryModelShortcut(historyModel, shortcutName)
createHistoryModelShortcut(
historyModel,
shortcutName
)
}

finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import android.app.Activity
import android.content.ComponentName
import android.os.Bundle
import com.sdex.activityrunner.app.launchActivity
import timber.log.Timber

class ShortcutHandlerActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val packageName = intent.getStringExtra(ARG_PACKAGE_NAME)
val className = intent.getStringExtra(ARG_CLASS_NAME)
val isExported = intent.getBooleanExtra(ARG_EXPORTED, false)
Timber.d("Shortcut: packageName=$packageName, className=$className")
if (packageName != null && className != null) {
val componentName = ComponentName(packageName, className)
launchActivity(componentName, isExported)
// keep it to support shortcuts created before #56
if (intent.hasExtra(ARG_EXPORTED)) {
val isExported = intent.getBooleanExtra(ARG_EXPORTED, false)
launchActivity(componentName, useRoot = !isExported)
} else {
val useRoot = intent.getBooleanExtra(ARG_USE_ROOT, false)
launchActivity(componentName, useRoot = useRoot)
}
}
finishAffinity()
}
Expand All @@ -24,5 +32,6 @@ class ShortcutHandlerActivity : Activity() {
const val ARG_PACKAGE_NAME = "arg_package_name"
const val ARG_CLASS_NAME = "arg_class_name"
const val ARG_EXPORTED = "arg_exported"
const val ARG_USE_ROOT = "arg_use_root"
}
}
12 changes: 9 additions & 3 deletions app/src/main/java/com/sdex/activityrunner/util/IntentUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ object IntentUtils {
}
}

fun createLauncherIcon(context: Context, activityModel: ActivityModel, bitmap: Bitmap?) {
fun createLauncherIcon(
context: Context,
activityModel: ActivityModel,
bitmap: Bitmap?,
useRoot: Boolean = false,
) {
if (bitmap != null) {
val intent = activityModel.toIntent(context)
val intent = activityModel.toIntent(context).apply {
putExtra(ShortcutHandlerActivity.ARG_USE_ROOT, useRoot)
}
val iconCompat = try {
IconCompat.createWithBitmap(bitmap)
} catch (e: Exception) { // android.os.TransactionTooLargeException
Expand All @@ -60,7 +67,6 @@ object IntentUtils {
val intent = getActivityIntent(Intent.ACTION_VIEW, component)
intent.putExtra(ShortcutHandlerActivity.ARG_PACKAGE_NAME, this.packageName)
intent.putExtra(ShortcutHandlerActivity.ARG_CLASS_NAME, this.className)
intent.putExtra(ShortcutHandlerActivity.ARG_EXPORTED, this.exported)
return intent
}

Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/layout/activity_add_shortcut.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@

</com.google.android.material.textfield.TextInputLayout>

<CheckBox
android:id="@+id/use_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="false"
android:text="@string/activity_option_launch_root"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/value_layout"
tools:visibility="visible" />

<Button
android:id="@+id/create"
style="?buttonBarButtonStyle"
Expand All @@ -68,7 +79,7 @@
android:layout_marginTop="8dp"
android:text="@android:string/cancel"
app:layout_constraintEnd_toStartOf="@+id/create"
app:layout_constraintTop_toBottomOf="@+id/value_layout" />
app:layout_constraintTop_toBottomOf="@+id/use_root" />

<ImageView
android:id="@+id/imageView"
Expand Down

0 comments on commit 0514f3b

Please sign in to comment.