Skip to content

Commit f8a3a8a

Browse files
committed
Add shortcut to logout secondary user
Fix cross profile intent filter database bug
1 parent a06692a commit f8a3a8a

8 files changed

Lines changed: 47 additions & 20 deletions

File tree

app/src/main/java/com/bintianqi/owndroid/MyDbHelper.kt

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import android.content.Context
44
import android.database.sqlite.SQLiteDatabase
55
import android.database.sqlite.SQLiteOpenHelper
66

7-
class MyDbHelper(context: Context): SQLiteOpenHelper(context, "data", null, 7) {
7+
class MyDbHelper(context: Context): SQLiteOpenHelper(context, "data", null, 8) {
88
override fun onCreate(db: SQLiteDatabase) {
99
db.execSQL(DHIZUKU_CLIENTS_TABLE)
1010
db.execSQL(SECURITY_LOGS_TABLE)
1111
db.execSQL(NETWORK_LOGS_TABLE)
1212
db.execSQL(APP_GROUPS_TABLE)
13-
db.execSQL(CP_INTENTS_TABLE)
13+
db.execSQL(CPIF_TABLE)
1414
}
1515
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
1616
if (oldVersion < 2) {
@@ -22,14 +22,8 @@ class MyDbHelper(context: Context): SQLiteOpenHelper(context, "data", null, 7) {
2222
if (oldVersion < 4) {
2323
db.execSQL(APP_GROUPS_TABLE)
2424
}
25-
if (oldVersion < 5) {
26-
db.execSQL(CP_INTENTS_TABLE)
27-
}
28-
if (oldVersion < 6) {
29-
db.execSQL(UPGRADE_CP_INTENTS_TABLE)
30-
}
31-
if (oldVersion < 7) {
32-
db.execSQL(RENAME_CP_COLUMN)
25+
if (oldVersion < 8) {
26+
db.execSQL(CPIF_TABLE)
3327
}
3428
}
3529
companion object {
@@ -43,11 +37,7 @@ class MyDbHelper(context: Context): SQLiteOpenHelper(context, "data", null, 7) {
4337
const val APP_GROUPS_TABLE = "CREATE TABLE app_groups(" +
4438
"id INTEGER PRIMARY KEY AUTOINCREMENT," +
4539
"name TEXT, apps TEXT)"
46-
const val CP_INTENTS_TABLE = "CREATE TABLE cross_profile_intent_filters (" +
40+
const val CPIF_TABLE = "CREATE TABLE cpif (" +
4741
"action_str TEXT, category TEXT, mime_type TEXT, direction INTEGER, time INTEGER)"
48-
const val UPGRADE_CP_INTENTS_TABLE = "ALTER TABLE cross_profile_intent_filters " +
49-
"ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0"
50-
const val RENAME_CP_COLUMN = "ALTER TABLE cross_profile_intent_filters " +
51-
"RENAME COLUMN created_at TO time"
5242
}
5343
}

app/src/main/java/com/bintianqi/owndroid/ShortcutsReceiverActivity.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bintianqi.owndroid
22

33
import android.app.Activity
4+
import android.os.Build
45
import android.os.Bundle
56
import android.util.Log
67
import com.bintianqi.owndroid.feature.users.UserOperationType
@@ -28,12 +29,14 @@ class ShortcutsReceiverActivity : Activity() {
2829
val state = dpm.getCameraDisabled(dar)
2930
dpm.setCameraDisabled(dar, !state)
3031
ShortcutUtils.setShortcut(context, sr, MyShortcut.DisableCamera, state)
32+
context.showOperationResultToast(true)
3133
}
3234

3335
"MUTE" -> {
3436
val state = dpm.isMasterVolumeMuted(dar)
3537
dpm.setMasterVolumeMuted(dar, !state)
3638
ShortcutUtils.setShortcut(context, sr, MyShortcut.Mute, state)
39+
context.showOperationResultToast(true)
3740
}
3841

3942
"USER_RESTRICTION" -> {
@@ -48,6 +51,7 @@ class ShortcutsReceiverActivity : Activity() {
4851
ShortcutUtils.updateUserRestrictionShortcut(
4952
context, sr, id, !state, false
5053
)
54+
context.showOperationResultToast(true)
5155
}
5256

5357
"USER_OPERATION" -> {
@@ -56,11 +60,18 @@ class ShortcutsReceiverActivity : Activity() {
5660
val serial = intent.getIntExtra("serial", -1)
5761
if (serial == -1) return@safeDpmCall
5862
doUserOperationWithContext(context, ph.dpm, ph.dar, type, serial, false)
63+
context.showOperationResultToast(true)
64+
}
65+
66+
"LOGOUT" -> {
67+
if (Build.VERSION.SDK_INT >= 28) {
68+
val result = dpm.logoutUser(dar)
69+
context.showOperationResultToast(result == 0)
70+
}
5971
}
6072
}
6173
}
6274
Log.d(TAG, "Received intent: $action")
63-
showOperationResultToast(true)
6475
} else {
6576
showOperationResultToast(false)
6677
}

app/src/main/java/com/bintianqi/owndroid/feature/users/UsersScreen.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,12 @@ fun UsersScreen(vm: UsersViewModel, onNavigateUp: () -> Unit, onNavigate: (Desti
144144
if (VERSION.SDK_INT >= 28 && dialog == 1) AlertDialog(
145145
title = { Text(stringResource(R.string.logout)) },
146146
text = {
147-
Text(stringResource(R.string.info_logout))
147+
Column {
148+
Text(stringResource(R.string.info_logout))
149+
TextButton({
150+
vm.requestPinLogoutShortcut()
151+
}) { Text(stringResource(R.string.create_shortcut)) }
152+
}
148153
},
149154
confirmButton = {
150155
TextButton({

app/src/main/java/com/bintianqi/owndroid/feature/users/UsersViewModel.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,9 @@ class UsersViewModel(
185185
val result = dpm.logoutUser(dar)
186186
if (result != 0) toastChannel.sendStatus(false)
187187
}
188+
189+
fun requestPinLogoutShortcut() {
190+
val result = ShortcutUtils.requestPinLogoutShortcut(application, settingsRepo)
191+
if (!result) toastChannel.sendStatus(false)
192+
}
188193
}

app/src/main/java/com/bintianqi/owndroid/feature/work_profile/CrossProfileIntentFilterRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class CrossProfileIntentFilterRepository(val dbHelper: MyDbHelper) {
1111
cv.put("mime_type", data.mimeType)
1212
cv.put("direction", data.direction)
1313
cv.put("time", System.currentTimeMillis())
14-
dbHelper.writableDatabase.insert("cross_profile_intent_filters", null, cv)
14+
dbHelper.writableDatabase.insert("cpif", null, cv)
1515
}
1616

1717
fun getAllCrossProfileIntentFilters(): List<IntentFilterOptions> {
1818
val list = mutableListOf<IntentFilterOptions>()
1919
dbHelper.readableDatabase.rawQuery(
20-
"SELECT * FROM cross_profile_intent_filters ORDER BY time DESC", null
20+
"SELECT * FROM cpif ORDER BY time DESC", null
2121
).use {
2222
while (it.moveToNext()) {
2323
list += IntentFilterOptions(
@@ -29,6 +29,6 @@ class CrossProfileIntentFilterRepository(val dbHelper: MyDbHelper) {
2929
}
3030

3131
fun deleteAllCrossProfileIntentFilters() {
32-
dbHelper.writableDatabase.delete("cross_profile_intent_filters", null, null)
32+
dbHelper.writableDatabase.delete("cpif", null, null)
3333
}
3434
}

app/src/main/java/com/bintianqi/owndroid/utils/ShortcutUtils.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,20 @@ object ShortcutUtils {
136136
context, shortcuts, context.getString(R.string.user_removed)
137137
)
138138
}
139+
140+
fun requestPinLogoutShortcut(context: Context, sr: SettingsRepository): Boolean {
141+
val shortcut = ShortcutInfoCompat.Builder(context, "LOGOUT")
142+
.setIcon(IconCompat.createWithResource(context, R.drawable.logout_fill0))
143+
.setShortLabel(context.getText(R.string.logout))
144+
.setIntent(
145+
Intent(context, ShortcutsReceiverActivity::class.java)
146+
.setAction("com.bintianqi.owndroid.action.LOGOUT")
147+
.putExtra("key", sr.data.shortcut.key)
148+
)
149+
.build()
150+
return ShortcutManagerCompat.requestPinShortcut(context, shortcut, null)
151+
}
152+
139153
fun setShortcutKey(sr: SettingsRepository) {
140154
if (sr.data.shortcut.key.isEmpty()) {
141155
sr.update {

app/src/main/res/values-zh-rCN/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@
511511
<string name="user_not_exist">用户不存在</string>
512512
<string name="serial_number">序列号</string>
513513
<string name="logout">登出</string>
514+
<string name="create_shortcut">创建快捷方式</string>
514515
<string name="start_in_background">在后台启动</string>
515516
<string name="user_operation_switch">切换</string>
516517
<string name="limit_reached">已达到上限</string>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@
547547
<string name="user_not_exist">User does not exist</string>
548548
<string name="serial_number">Serial number</string>
549549
<string name="logout">Logout</string>
550+
<string name="create_shortcut">Create shortcut</string>
550551
<string name="start_in_background">Start in background</string>
551552
<string name="user_operation_switch">Switch</string>
552553
<string name="limit_reached">Limit reached</string>

0 commit comments

Comments
 (0)