Skip to content

Commit

Permalink
Drop requirement for android.permission.QUERY_ALL_PACKAGES (#106)
Browse files Browse the repository at this point in the history
Drop need for QUERY_ALL_PACKAGES without dropping any features

Fix #101
  • Loading branch information
Kaljurand committed Apr 9, 2023
1 parent 206e822 commit 790f73e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ android {
applicationId 'ee.ioc.phon.android.speak'
minSdkVersion 23
targetSdkVersion 33
versionCode 1900
versionName '1.9.00'
versionCode 1901
versionName '1.9.01'
vectorDrawables.useSupportLibrary = true
// Keep only en and et resources
resConfigs 'en', 'et'
Expand Down Expand Up @@ -72,7 +72,7 @@ android {
lint {
// TODO: in the future check for Kotlin-Java interop
//check 'Interoperability'
disable 'ResourceType', 'AppLinkUrlError', 'EllipsizeMaxLines', 'RtlSymmetry', 'Autofill', 'QueryAllPackagesPermission'
disable 'ResourceType', 'AppLinkUrlError', 'EllipsizeMaxLines', 'RtlSymmetry', 'Autofill'
}
namespace 'ee.ioc.phon.android.speak'
}
Expand Down
14 changes: 4 additions & 10 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@
mute the audio stream during recognition. -->
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

<!-- TODO: try to avoid having to request this permission (and then enable QueryAllPackagesPermission)
Android v11. We query packages for several reasons:
1) finding speech recognizer providers (this query is declared below in the queries-section)
2) finding TTS service providers (this query is declared below in the queries-section)
3) listing apps (with icon and launch intent) that have previously launched Kõnele
4) launching intents (e.g. setting the alarm or opening maps) via rewrites
It is unclear if use-cases 3 and 4 could be instead declared via the queries-section.
-->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<!-- Custom permission required by GetPutPreferenceActivity to be able to read/write the Kõnele settings. -->
<!--
<uses-permission android:name="ee.ioc.phon.android.speak.permission.GET_PUT_SETTING" />
Expand Down Expand Up @@ -96,6 +86,10 @@
<!-- Packages that provide TTS services -->
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
<intent>
<!-- This seems to be needed to be able to show the labels and icons of apps, queried by package name. -->
<action android:name="android.intent.action.MAIN" />
</intent>
</queries>

<application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2011, Institute of Cybernetics at Tallinn University of Technology
* Copyright 2011-2023, Institute of Cybernetics at Tallinn University of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,10 +18,10 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -58,18 +58,16 @@ public AppListCursorAdapter(Context context, Cursor c, boolean autoRequery) {
public void bindView(View view, Context context, Cursor c) {
String packageName = c.getString(c.getColumnIndexOrThrow(App.Columns.FNAME));

String label = "";
Drawable icon = null;
ApplicationInfo ai = null;
try {
label = mPm.getApplicationLabel(mPm.getApplicationInfo(packageName, 0)).toString();
icon = mPm.getApplicationIcon(packageName);
ai = mPm.getApplicationInfo(packageName, 0);
} catch (NameNotFoundException e) {
// Intentionally empty
// The given package has been uninstalled from the device
}

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

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

// App icon (can be null if the app has been uninstalled)
ImageView itemAppIcon = view.findViewById(R.id.itemAppIcon);
if (icon == null) {
if (ai == null) {
itemAppIcon.setVisibility(View.INVISIBLE);
} else {
itemAppIcon.setVisibility(View.VISIBLE);
itemAppIcon.setImageDrawable(icon);
itemAppIcon.setImageDrawable(mPm.getApplicationIcon(ai));
}

// Grammar URL assigned to the app (comes from the DB)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ class ChatDemoActivity : AppCompatActivity() {
private var mRewriters: Iterable<UtteranceRewriter>? = null

override fun onComboChange(language: String, service: ComponentName) {
mRewriters = Utils.genRewriters(mPrefs, mRes, arrayOf("Base", "Commands"),
CommandMatcherFactory.createCommandFilter(language, service, componentName))
mRewriters = Utils.genRewriters(
mPrefs, mRes, arrayOf("Base", "Commands"),
CommandMatcherFactory.createCommandFilter(language, service, componentName)
)
}

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

(findViewById(android.R.id.list) as ListView).onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ ->
val entry = parent.adapter.getItem(position)
startActivity(entry.toString())
}
(findViewById<ListView>(android.R.id.list)).onItemClickListener =
AdapterView.OnItemClickListener { parent, _, position, _ ->
val entry = parent.adapter.getItem(position)
startActivity(entry.toString())
}
}

private fun startActivity(intentAsJson: String) {
try {
IntentUtils.startActivityIfAvailable(this, JsonUtils.createIntent(intentAsJson))
IntentUtils.startActivity(this, JsonUtils.createIntent(intentAsJson))
} catch (e: JSONException) {
toast(e.localizedMessage)
} catch (e: SecurityException) {
toast(e.localizedMessage)
}
}

private fun updateListView(list: List<String>) {
(findViewById(android.R.id.list) as ListView).adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
(findViewById<ListView>(android.R.id.list)).adapter =
ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
}

private fun toast(message: String) {
Expand Down

0 comments on commit 790f73e

Please sign in to comment.