Skip to content

Commit

Permalink
Add weather command, remove unused permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
devycarol committed Aug 3, 2024
1 parent cc2a145 commit ce0113f
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 23 deletions.
8 changes: 5 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> <!-- TODO just use file selection -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

<queries>
Expand Down Expand Up @@ -66,6 +63,11 @@

<category android:name="android.intent.category.APP_CALCULATOR" />
</intent>
<intent> <!-- Opening calculator apps -->
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.APP_WEATHER" />
</intent>
</queries>

<application
Expand Down
46 changes: 46 additions & 0 deletions app/src/main/java/net/emilla/commands/CatCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.emilla.commands;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import androidx.annotation.StringRes;

import net.emilla.AssistActivity;
import net.emilla.utils.Apps;
import net.emilla.utils.Dialogs;

import java.util.List;

public abstract class CatCommand extends CoreCommand {
private final int mAppCount;
private Intent mLaunchIntent;
private AlertDialog mAppChooser;

public CatCommand(final AssistActivity act, @StringRes final int nameId,
@StringRes final int instructionId, final String category) {
super(act, nameId, instructionId);

final PackageManager pm = act.getPackageManager();
final List<ResolveInfo> appList = Apps.resolveList(pm, category);
mAppCount = appList.size();
if (mAppCount == 1) mLaunchIntent = Apps.launchIntent(appList.get(0).activityInfo);
else if (mAppCount > 1) mAppChooser = Dialogs.appChooser(act, pm, appList).create();
}

protected abstract void noSuchApp(); // TODO: handle at mapping

@Override
public void run() {
switch (mAppCount) {
case 0 -> noSuchApp();
case 1 -> succeed(mLaunchIntent);
default -> offer(mAppChooser);
// todo: allow to select a default app, ensuring that the preference is cleared if ever the default is no longer installed or a new candidate is installed
// interestingly, Tasker is included if you remove CATEGORY_LAUNCHER from the intent. i assume this is for its special shortcut functionality.
// will keep an eye on this. it shouldn't be included in this dialog (by default it "succeeds" to no actual activity, which was confusing to debug lol)
// but it would be pretty useful to have a toolchain of those special launches ifwhen a dedicated "tasker" command is added.
}
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/net/emilla/commands/CatCommandCalculate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package net.emilla.commands;

import static android.content.Intent.CATEGORY_APP_CALCULATOR;

import net.emilla.AssistActivity;
import net.emilla.R;
import net.emilla.exceptions.EmlaAppsException;
import net.emilla.parsing.Calculator;

public class CatCommandCalculate extends CatCommand {
public CatCommandCalculate(final AssistActivity act) {
super(act, R.string.command_calculate, R.string.instruction_calculate, CATEGORY_APP_CALCULATOR);
}

@Override
protected void noSuchApp() {
throw new EmlaAppsException("No calculator app found for your device.");
}

@Override
public Command cmd() {
return Command.CALCULATE;
}

@Override
public void run(final String expression) {
// todo: AOSP calculator doesn't support piping in text, but maybe others do via ACTION_SEND?
// i think run-with-instruction should still be a custom implementation, but you could add special support for such apps with a simple AppSendCommand
give(String.valueOf(Calculator.compute(expression)), true);
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/net/emilla/commands/CatCommandWeather.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.emilla.commands;

import static android.content.Intent.CATEGORY_APP_WEATHER;

import android.os.Build;

import net.emilla.AssistActivity;
import net.emilla.R;
import net.emilla.exceptions.EmlaAppsException;
import net.emilla.exceptions.EmlaBadCommandException;

public class CatCommandWeather extends CatCommand {
public CatCommandWeather(final AssistActivity act) {
super(act, R.string.command_weather, R.string.instruction_app, CATEGORY_APP_WEATHER);
}

@Override
protected void noSuchApp() {
throw new EmlaAppsException("No weather app found for your device.");
}

@Override
public Command cmd() {
return Command.WEATHER;
}

@Override
public void run() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) throw new EmlaBadCommandException("Sorry! This command doesn't support your Android version yet."); // Todo
super.run();
}

@Override
public void run(final String expression) {
throw new EmlaBadCommandException("Sorry! I don't have categorical app search yet."); // Todo
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/net/emilla/commands/CommandLaunch.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void run(final String app) {
otherIntents = null;

prefLabels[0] = label;
prefIntents[0] = Apps.launcherIntent(packageName, info.name);
prefIntents[0] = Apps.launchIntent(packageName, info.name);
prefCount = 1;

for (++i; i < appCount; ++i) { // continue searching for duplicates only
Expand All @@ -77,7 +77,7 @@ public void run(final String app) {

if (lcLabel.equals(lcQuery)) {
prefLabels[prefCount] = label;
prefIntents[prefCount] = Apps.launcherIntent(info.packageName, info.name);
prefIntents[prefCount] = Apps.launchIntent(info.packageName, info.name);
++prefCount;
}
}
Expand All @@ -86,7 +86,7 @@ public void run(final String app) {
break; // search is finished
}
if (lcLabel.contains(lcQuery)) {
final Intent in = Apps.launcherIntent(packageName, info.name);
final Intent in = Apps.launchIntent(packageName, info.name);
if (lcLabel.startsWith(lcQuery)) {
prefLabels[prefCount] = label;
prefIntents[prefCount] = in;
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/net/emilla/commands/EmillaCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum Command {
CONTACT,
NOTIFY,
CALCULATE,
WEATHER,
VIEW,
TOAST,
APP,
Expand Down Expand Up @@ -72,6 +73,7 @@ public enum Command {
R.string.command_contact,
R.string.command_notify,
R.string.command_calculate,
R.string.command_weather,
R.string.command_view,
R.string.command_toast
};
Expand Down Expand Up @@ -133,7 +135,7 @@ public static CommandTree tree(final AssistActivity act, final SharedPreferences
final ActivityInfo info = ri.activityInfo;
final CharSequence label = info.loadLabel(pm);
final String pkg = info.packageName;
final Intent launch = Apps.launcherIntent(pkg, info.name);
final Intent launch = Apps.launchIntent(pkg, info.name);
final AppCommand appCmd = getAppCmd(act, pm, res, pkg, cmdTree, label, launch);
final String lcLabel = label.toString().toLowerCase();
cmdTree.put(lcLabel, appCmd, act);
Expand Down Expand Up @@ -163,9 +165,10 @@ public static Command command(
public static boolean usesData(final Command cmd) {
return switch (cmd) {
case DEFAULT -> usesData(DEFAULT_CMD);
case CALL, DIAL, LAUNCH, SETTINGS, WEB, FIND, CLOCK, CALCULATE, VIEW, APP_SEND, APP -> false;
case CALL, DIAL, LAUNCH, SETTINGS, WEB, FIND, CLOCK, CALCULATE, WEATHER, VIEW, APP_SEND, APP
-> false;
case SMS, EMAIL, SHARE, NOTE, TODO, ALARM, TIMER, POMODORO, CALENDAR, CONTACT, NOTIFY, TOAST,
DUPLICATE, APP_SEND_DATA -> true;
DUPLICATE, APP_SEND_DATA -> true;
};
}

Expand All @@ -175,12 +178,12 @@ public static int imeAction(final Command cmd) {
// requires changing the input method code directly
if (usesData(cmd)) return IME_ACTION_NEXT;
return switch (cmd) {
case CALL, DIAL, LAUNCH, VIEW, APP -> IME_ACTION_GO;
case CALL, DIAL, LAUNCH, WEATHER, VIEW, APP -> IME_ACTION_GO;
case WEB, FIND -> IME_ACTION_SEARCH;
case APP_SEND -> IME_ACTION_SEND; // todo: this shouldn't apply when just launching and also not to the newpipes
case SETTINGS, CLOCK, CALCULATE -> IME_ACTION_DONE;
case DEFAULT, SMS, EMAIL, SHARE, NOTE, TODO, ALARM, TIMER, POMODORO, CALENDAR, CONTACT, NOTIFY,
TOAST, DUPLICATE, APP_SEND_DATA -> -1;
TOAST, DUPLICATE, APP_SEND_DATA -> -1;
};
}

Expand All @@ -189,7 +192,8 @@ private static boolean shouldLowercase(final Command cmd) {
// Please let me know if this should vary for your locale.
return switch (cmd) {
case DEFAULT, CALL, DIAL, EMAIL, SHARE, LAUNCH, SETTINGS, NOTE, TODO, WEB, FIND, CLOCK, ALARM,
TIMER, POMODORO, CALENDAR, CONTACT, NOTIFY, CALCULATE, VIEW, TOAST, DUPLICATE -> true;
TIMER, POMODORO, CALENDAR, CONTACT, NOTIFY, CALCULATE, WEATHER, VIEW, TOAST, DUPLICATE
-> true;
case SMS, APP, APP_SEND, APP_SEND_DATA -> false;
};
}
Expand All @@ -212,7 +216,7 @@ private static int detailsId(final Command cmd) {
case DUPLICATE -> R.array.details_duplicate;
case APP_SEND -> R.array.details_app_send; // todo: shouldn't apply to newpipes
case APP_SEND_DATA -> R.array.details_app_send_data;
case LAUNCH, TODO, WEB, FIND, CLOCK, NOTIFY, CALCULATE, VIEW, APP -> -1;
case LAUNCH, TODO, WEB, FIND, CLOCK, NOTIFY, CALCULATE, WEATHER, VIEW, APP -> -1;
};
}

Expand All @@ -236,8 +240,8 @@ public static CharSequence dataHint(final Resources res, final Command cmd) {
case NOTIFY -> R.string.data_hint_notify;
case TOAST -> R.string.data_hint_toast;
case APP_SEND_DATA -> R.string.data_hint_app_send_data;
case DEFAULT, CALL, DIAL, LAUNCH, SETTINGS, WEB, FIND, CLOCK, CALCULATE, VIEW, DUPLICATE,
APP_SEND, APP -> R.string.data_hint_default;
case DEFAULT, CALL, DIAL, LAUNCH, SETTINGS, WEB, FIND, CLOCK, CALCULATE, WEATHER, VIEW,
DUPLICATE, APP_SEND, APP -> R.string.data_hint_default;
};
return res.getString(hintId);
}
Expand All @@ -264,6 +268,7 @@ public static int icon(Command cmd) {
case CONTACT -> R.drawable.ic_contact;
case NOTIFY -> R.drawable.ic_notify;
case CALCULATE -> R.drawable.ic_calculate;
case WEATHER -> R.drawable.ic_weather;
case VIEW -> R.drawable.ic_view;
case TOAST -> R.drawable.ic_toast;
case DUPLICATE -> R.drawable.ic_command;
Expand Down Expand Up @@ -292,7 +297,8 @@ private static EmillaCommand instance(final Command cmd, final AssistActivity ac
case CALENDAR -> new CommandCalendar(act);
case CONTACT -> new CommandContact(act);
case NOTIFY -> new CommandNotify(act);
case CALCULATE -> new CommandCalculate(act);
case CALCULATE -> new CatCommandCalculate(act);
case WEATHER -> new CatCommandWeather(act);
case VIEW -> new CommandView(act);
case TOAST -> new CommandToast(act);
case APP, APP_SEND, APP_SEND_DATA, DUPLICATE -> null; // uuuhhhhhhh
Expand Down Expand Up @@ -321,7 +327,8 @@ public static EmillaCommand instance(final AssistActivity act, final Command cmd
case CALENDAR -> new CommandCalendar(act);
case CONTACT -> new CommandContact(act);
case NOTIFY -> new CommandNotify(act);
case CALCULATE -> new CommandCalculate(act);
case CALCULATE -> new CatCommandCalculate(act);
case WEATHER -> new CatCommandWeather(act);
case VIEW -> new CommandView(act);
case TOAST -> new CommandToast(act);
case DUPLICATE, APP_SEND, APP_SEND_DATA -> appMap.get(name.toString().toLowerCase());
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/net/emilla/config/CommandsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private void setupCores(final EmillaActivity act, final SharedPreferences prefs,
setupCorePref("aliases_contact_text", listener, prefs, res, R.array.aliases_contact);
deactivate("aliases_notify_text", dListener);
setupCorePref("aliases_calculate_text", listener, prefs, res, R.array.aliases_calculate);
setupCorePref("aliases_weather_text", listener, prefs, res, R.array.aliases_weather);
setupCorePref("aliases_view_text", listener, prefs, res, R.array.aliases_view);
setupCorePref("aliases_toast_text", listener, prefs, res, R.array.aliases_toast);
deactivate("aliases_custom_text", dListener);
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/net/emilla/settings/Aliases.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Aliases {
R.array.aliases_contact,
R.array.aliases_notify,
R.array.aliases_calculate,
R.array.aliases_weather,
R.array.aliases_view,
R.array.aliases_toast
};
Expand All @@ -52,6 +53,7 @@ public class Aliases {
"aliases_contact",
"aliases_notify",
"aliases_calculate",
"aliases_weather",
"aliases_view",
"aliases_toast"
};
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/net/emilla/utils/Apps.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@ public static List<ResolveInfo> resolveList(final PackageManager pm) {
return pm.queryIntentActivities(new Intent(ACTION_MAIN).addCategory(CATEGORY_LAUNCHER), 0);
}

public static Intent launcherIntent(final String pkg, final String cls) {
@NonNull
public static List<ResolveInfo> resolveList(final PackageManager pm, final String category) {
return pm.queryIntentActivities(categoryIntent(category), 0);
}

public static Intent launchIntent(final String pkg, final String cls) {
final ComponentName cn = new ComponentName(pkg, cls);
return new Intent(ACTION_MAIN).addCategory(CATEGORY_LAUNCHER).setPackage(pkg).setComponent(cn)
.addFlags(FLAG_ACTIVITY_NEW_TASK);
}

public static Intent launcherIntent(final ActivityInfo info) {
return launcherIntent(info.packageName, info.name);
public static Intent launchIntent(final ActivityInfo info) {
return launchIntent(info.packageName, info.name);
}

public static Intent newTask(final String action) {
Expand All @@ -57,6 +62,10 @@ public static Intent newTask(final String action, final Uri data, final String t
return new Intent(action).setDataAndType(data, type).addFlags(FLAG_ACTIVITY_NEW_TASK);
}

public static Intent categoryIntent(final String category) {
return new Intent(ACTION_MAIN).addCategory(CATEGORY_LAUNCHER).addCategory(category);
}

public static Intent sendTask(final String pkg) {
// Todo: attachments
return new Intent(ACTION_SEND).setType("text/plain").setPackage(pkg)
Expand All @@ -82,10 +91,10 @@ public static CharSequence[] labels(final List<ResolveInfo> appList, final Packa

public static Intent[] intents(final List<ResolveInfo> appList) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) return appList.parallelStream()
.map(ri -> launcherIntent(ri.activityInfo)).toArray(Intent[]::new);
.map(ri -> launchIntent(ri.activityInfo)).toArray(Intent[]::new);
final Intent[] intents = new Intent[appList.size()];
int i = -1;
for (final ResolveInfo ri : appList) intents[++i] = launcherIntent(ri.activityInfo);
for (final ResolveInfo ri : appList) intents[++i] = launchIntent(ri.activityInfo);
return intents;
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/drawable/ic_calculate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#FFF"
android:pathData="M7,2H17A2,2 0 0,1 19,4V20A2,2 0 0,1 17,22H7A2,2 0 0,1 5,20V4A2,2 0 0,1 7,2M7,4V8H17V4H7M7,10V12H9V10H7M11,10V12H13V10H11M15,10V12H17V10H15M7,14V16H9V14H7M11,14V16H13V14H11M15,14V16H17V14H15M7,18V20H9V18H7M11,18V20H13V18H11M15,18V20H17V18H15Z" />
android:pathData="M7,2H17A2,2 0 0,1 19,4V20A2,2 0 0,1 17,22H7A2,2 0 0,1 5,20V4A2,2 0 0,1 7,2M7,4V8H17V4H7M7,10V12H9V10H7M11,10V12H13V10H11M15,10V12H17V10H15M7,14V16H9V14H7M11,14V16H13V14H11M15,14V16H17V14H15M7,18V20H9V18H7M11,18V20H13V18H11M15,18V20H17V18H15Z"/>
</vector>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/ic_weather.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#FFF"
android:pathData="M6.76,4.84l-1.8,-1.79 -1.41,1.41 1.79,1.79 1.42,-1.41zM4,10.5L1,10.5v2h3v-2zM13,0.55h-2L11,3.5h2L13,0.55zM20.45,4.46l-1.41,-1.41 -1.79,1.79 1.41,1.41 1.79,-1.79zM17.24,18.16l1.79,1.8 1.41,-1.41 -1.8,-1.79 -1.4,1.4zM20,10.5v2h3v-2h-3zM12,5.5c-3.31,0 -6,2.69 -6,6s2.69,6 6,6 6,-2.69 6,-6 -2.69,-6 -6,-6zM11,22.45h2L13,19.5h-2v2.95zM3.55,18.54l1.41,1.41 1.79,-1.8 -1.41,-1.41 -1.79,1.8z"/>
</vector>
3 changes: 3 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<item>calc</item>
<item>math</item>
</string-array>
<string-array name="aliases_weather">
<item>wea</item>
</string-array>
<string-array name="aliases_view">
<item>look</item>
<item>see</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<string name="command_contact">Contact</string>
<string name="command_notify">Notify</string>
<string name="command_calculate">Calculate</string>
<string name="command_weather">Weather</string>
<string name="command_view">View</string>
<string name="command_toast">Toast</string>
<string name="command_custom">Custom</string>
Expand All @@ -126,6 +127,7 @@
<string name="summary_contact">View, create, and edit contacts</string>
<string name="summary_notify">Set notification reminders</string>
<string name="summary_calculate">Compute math equations</string>
<string name="summary_weather">Open a weather app</string>
<string name="summary_view">Open bookmarked URLs</string><!--todo: other url types. also "play"—music, audiobooks, and podcasts-->
<string name="summary_toast">Show a little message!</string>
<string name="summary_custom">Create your own commands!</string>
Expand Down
Loading

0 comments on commit ce0113f

Please sign in to comment.