Skip to content

Commit 790f73e

Browse files
authoredApr 9, 2023
Drop requirement for android.permission.QUERY_ALL_PACKAGES (#106)
Drop need for QUERY_ALL_PACKAGES without dropping any features Fix #101
1 parent 206e822 commit 790f73e

File tree

5 files changed

+30
-32
lines changed

5 files changed

+30
-32
lines changed
 

‎app/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ android {
2929
applicationId 'ee.ioc.phon.android.speak'
3030
minSdkVersion 23
3131
targetSdkVersion 33
32-
versionCode 1900
33-
versionName '1.9.00'
32+
versionCode 1901
33+
versionName '1.9.01'
3434
vectorDrawables.useSupportLibrary = true
3535
// Keep only en and et resources
3636
resConfigs 'en', 'et'
@@ -72,7 +72,7 @@ android {
7272
lint {
7373
// TODO: in the future check for Kotlin-Java interop
7474
//check 'Interoperability'
75-
disable 'ResourceType', 'AppLinkUrlError', 'EllipsizeMaxLines', 'RtlSymmetry', 'Autofill', 'QueryAllPackagesPermission'
75+
disable 'ResourceType', 'AppLinkUrlError', 'EllipsizeMaxLines', 'RtlSymmetry', 'Autofill'
7676
}
7777
namespace 'ee.ioc.phon.android.speak'
7878
}

‎app/src/main/AndroidManifest.xml

+4-10
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@
4040
mute the audio stream during recognition. -->
4141
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />
4242

43-
<!-- TODO: try to avoid having to request this permission (and then enable QueryAllPackagesPermission)
44-
Android v11. We query packages for several reasons:
45-
1) finding speech recognizer providers (this query is declared below in the queries-section)
46-
2) finding TTS service providers (this query is declared below in the queries-section)
47-
3) listing apps (with icon and launch intent) that have previously launched Kõnele
48-
4) launching intents (e.g. setting the alarm or opening maps) via rewrites
49-
It is unclear if use-cases 3 and 4 could be instead declared via the queries-section.
50-
-->
51-
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
52-
5343
<!-- Custom permission required by GetPutPreferenceActivity to be able to read/write the Kõnele settings. -->
5444
<!--
5545
<uses-permission android:name="ee.ioc.phon.android.speak.permission.GET_PUT_SETTING" />
@@ -96,6 +86,10 @@
9686
<!-- Packages that provide TTS services -->
9787
<action android:name="android.intent.action.TTS_SERVICE" />
9888
</intent>
89+
<intent>
90+
<!-- This seems to be needed to be able to show the labels and icons of apps, queried by package name. -->
91+
<action android:name="android.intent.action.MAIN" />
92+
</intent>
9993
</queries>
10094

10195
<application

‎app/src/main/java/ee/ioc/phon/android/speak/AppListCursorAdapter.java

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011, Institute of Cybernetics at Tallinn University of Technology
2+
* Copyright 2011-2023, Institute of Cybernetics at Tallinn University of Technology
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,10 @@
1818

1919
import android.content.Context;
2020
import android.content.SharedPreferences;
21+
import android.content.pm.ApplicationInfo;
2122
import android.content.pm.PackageManager;
2223
import android.content.pm.PackageManager.NameNotFoundException;
2324
import android.database.Cursor;
24-
import android.graphics.drawable.Drawable;
2525
import android.preference.PreferenceManager;
2626
import android.view.LayoutInflater;
2727
import android.view.View;
@@ -58,18 +58,16 @@ public AppListCursorAdapter(Context context, Cursor c, boolean autoRequery) {
5858
public void bindView(View view, Context context, Cursor c) {
5959
String packageName = c.getString(c.getColumnIndexOrThrow(App.Columns.FNAME));
6060

61-
String label = "";
62-
Drawable icon = null;
61+
ApplicationInfo ai = null;
6362
try {
64-
label = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString();
65-
icon = mPm.getApplicationIcon(packageName);
63+
ai = mPm.getApplicationInfo(packageName, 0);
6664
} catch (NameNotFoundException e) {
67-
// Intentionally empty
65+
// The given package has been uninstalled from the device
6866
}
6967

7068
// App label which can be "" if the app has been uninstalled.
7169
TextView itemAppName = view.findViewById(R.id.itemAppName);
72-
itemAppName.setText(label);
70+
itemAppName.setText(ai == null ? "" : mPm.getApplicationLabel(ai));
7371

7472
// App package name (comes from the DB)
7573
TextView itemAppFname = view.findViewById(R.id.itemAppFname);
@@ -81,11 +79,11 @@ public void bindView(View view, Context context, Cursor c) {
8179

8280
// App icon (can be null if the app has been uninstalled)
8381
ImageView itemAppIcon = view.findViewById(R.id.itemAppIcon);
84-
if (icon == null) {
82+
if (ai == null) {
8583
itemAppIcon.setVisibility(View.INVISIBLE);
8684
} else {
8785
itemAppIcon.setVisibility(View.VISIBLE);
88-
itemAppIcon.setImageDrawable(icon);
86+
itemAppIcon.setImageDrawable(mPm.getApplicationIcon(ai));
8987
}
9088

9189
// Grammar URL assigned to the app (comes from the DB)

‎app/src/main/java/ee/ioc/phon/android/speak/demo/ChatDemoActivity.kt

+14-8
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ class ChatDemoActivity : AppCompatActivity() {
5757
private var mRewriters: Iterable<UtteranceRewriter>? = null
5858

5959
override fun onComboChange(language: String, service: ComponentName) {
60-
mRewriters = Utils.genRewriters(mPrefs, mRes, arrayOf("Base", "Commands"),
61-
CommandMatcherFactory.createCommandFilter(language, service, componentName))
60+
mRewriters = Utils.genRewriters(
61+
mPrefs, mRes, arrayOf("Base", "Commands"),
62+
CommandMatcherFactory.createCommandFilter(language, service, componentName)
63+
)
6264
}
6365

6466
override fun onFinalResult(results: List<String>, bundle: Bundle) {
@@ -96,22 +98,26 @@ class ChatDemoActivity : AppCompatActivity() {
9698
siv.init(R.array.keysActivity, callerInfo, false, null)
9799
siv.setListener(speechInputViewListener, null)
98100

99-
(findViewById(android.R.id.list) as ListView).onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ ->
100-
val entry = parent.adapter.getItem(position)
101-
startActivity(entry.toString())
102-
}
101+
(findViewById<ListView>(android.R.id.list)).onItemClickListener =
102+
AdapterView.OnItemClickListener { parent, _, position, _ ->
103+
val entry = parent.adapter.getItem(position)
104+
startActivity(entry.toString())
105+
}
103106
}
104107

105108
private fun startActivity(intentAsJson: String) {
106109
try {
107-
IntentUtils.startActivityIfAvailable(this, JsonUtils.createIntent(intentAsJson))
110+
IntentUtils.startActivity(this, JsonUtils.createIntent(intentAsJson))
108111
} catch (e: JSONException) {
109112
toast(e.localizedMessage)
113+
} catch (e: SecurityException) {
114+
toast(e.localizedMessage)
110115
}
111116
}
112117

113118
private fun updateListView(list: List<String>) {
114-
(findViewById(android.R.id.list) as ListView).adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
119+
(findViewById<ListView>(android.R.id.list)).adapter =
120+
ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
115121
}
116122

117123
private fun toast(message: String) {

0 commit comments

Comments
 (0)
Failed to load comments.