Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/quick_actions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.2

* Fixing the quick actions launch on Android when the app is killed.

## 0.3.1

* Added unit tests.
Expand Down
13 changes: 9 additions & 4 deletions packages/quick_actions/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.flutter.plugins.quickactions">
xmlns:tools="http://schemas.android.com/tools"
package="io.flutter.plugins.quickactions">

<application>
<activity android:name=".QuickActionsPlugin$ShortcutHandlerActivity" android:exported="false"/>
</application>
<application>
<activity
android:name=".QuickActionsPlugin$ShortcutHandlerActivity"
android:exported="false"
android:relinquishTaskIdentity="true"
tools:targetApi="lollipop" />

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curious, what is this for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need tools:targetApi to hide a warning in android studio regarding android:relinquishTaskIdentity — the previous one. We can use targetApi — lollipop as the plugin anyways works on devices from API at least 25. Also, in case, if the app is killed and we open it using the shortcut, it will work fine but only the first time. If you click the Home button after that and use the same shortcut again it will not work the second and following times without android:relinquishTaskIdentity.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried android:noHistory="true" ? As android:relinquishTaskIdentity="true" works only on API +25, all users in APIs below that will not have the same behavior.

@diesersamat diesersamat Jul 14, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly the same API version Android shortcuts and its classes were added ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh shortcuts are API 25+. So there is no problem.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will wait the merge to make another PR. I found that some more changes are needed. Sorry for the trouble.

</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
Expand Down Expand Up @@ -109,6 +110,10 @@ private List<ShortcutInfo> deserializeShortcuts(List<Map<String, String>> shortc
*/
public static class ShortcutHandlerActivity extends Activity {

private final String sharedPreferencesName = "FlutterSharedPreferences";
private final String sharedPreferencesKeyPrefix = "flutter.";
private final String sharedPreferencesActionKey = "action";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -117,8 +122,31 @@ protected void onCreate(Bundle savedInstanceState) {
String type = intent.getStringExtra("type");
if (channel != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran your code and this channel check will make the opening fail when channel != null.
Steps to reproduce:

  1. Install and open the app normally.
  2. Minimize the app by tapping in the home button.
  3. Use the app shortcut.
  4. Exit the app using the back button. ## Not the home button.
  5. Use the app shortcut. ## "nothing happens".

Possible fix that is not a breaking change:
In ShortcutHandlerActivity class :

      // Get the Intent that started this activity and extract the string
      Intent intent = getIntent();
      String type = intent.getStringExtra("type");
      startActivity(getIntentToOpenMainActivity(this, type));

      finish();
        launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        launchIntentForPackage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

@diesersamat diesersamat Jul 14, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I know about this issue and it isn't something I wanted to fix in this PR. If you can, please, check the current version of the plugin and make sure that the bug was there before my fix. I don't know if I should fix it in this PR, maybe you can open an issue or even submit a PR? Or do you think that it's better to fix the bug in this PR? Anyway, I will check your solution, because I also faced this issue and was thinking about the ways to fix it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@collinjackson ☝️ ? Another PR? It seems small enough to land together with this version.

@diesersamat diesersamat Jul 15, 2019

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BugsBunnyBR I know why does this happen. When you click the "Back" button, the activity is being killed. So, the activity is killed now, but channel reference and QuickActionsPlugin class instance is still alive. So, later, when you click on the shortcut, it tries to send a message to the channel, because it is not null — here is the difference with the bug fixed in this PR. But as you know, the view is already detached, and you'll get only this message in logcat FlutterView.send called on a detached view, channel=. Unfortunately, you cannot put a callback there to react on this error case and, maybe, start the activity again, because of API design. I've opened an issue a week ago flutter/flutter#35628 but maybe it is possible to fix without changes in FlutterNativeView API design...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I know how to solve the problem. Just remove the static for the channel. In general, having static fields that are associated with context are a bad ideia. But removing the static makes that we need some other few changes, like listening to onNewIntent, that is why I think another pr will be needed to have a proper handling.

channel.invokeMethod("launch", type);
} else {
setActionToSharedPreferences(type);
startActivity(getIntentToOpenMainActivity(this));
}
finish();
}

/**
* Returns Intent to launch the MainActivity. Used to start the app, if one of quick actions was
* called from the background.
*/
private Intent getIntentToOpenMainActivity(Context context) {
return context
.getPackageManager()
.getLaunchIntentForPackage(context.getApplicationContext().getPackageName());
}

/**
* Saving action name to shared preferences to run requested quick action later, after app
* launch
*/
private void setActionToSharedPreferences(String type) {
SharedPreferences prefs =
this.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
prefs.edit().putString(sharedPreferencesKeyPrefix + sharedPreferencesActionKey, type).apply();
}
}
}
21 changes: 20 additions & 1 deletion packages/quick_actions/lib/quick_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import 'dart:async';

import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:shared_preferences/shared_preferences.dart';

const MethodChannel _kChannel =
MethodChannel('plugins.flutter.io/quick_actions');

const String _actionSharedPrefsKey = "action";

/// Handler for a quick action launch event.
///
/// The argument [type] corresponds to the [ShortcutItem]'s field.
Expand Down Expand Up @@ -49,11 +52,27 @@ class QuickActions {
/// Initializes this plugin.
///
/// Call this once before any further interaction with the the plugin.
void initialize(QuickActionHandler handler) {
void initialize(QuickActionHandler handler) async {
channel.setMethodCallHandler((MethodCall call) async {
assert(call.method == 'launch');
handler(call.arguments);
});

final String action = await _getActionFromSharedPreferences();
if (action != null) {
handler(action);
_removeActionFromSharedPreferences();
}
}

Future<String> _getActionFromSharedPreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.getString(_actionSharedPrefsKey);
}

Future<void> _removeActionFromSharedPreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return prefs.setString(_actionSharedPrefsKey, null);
}

/// Sets the [ShortcutItem]s to become the app's quick actions.
Expand Down
3 changes: 2 additions & 1 deletion packages/quick_actions/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for creating shortcuts on home screen, also known as
Quick Actions on iOS and App Shortcuts on Android.
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/quick_actions
version: 0.3.1
version: 0.3.2

flutter:
plugin:
Expand All @@ -15,6 +15,7 @@ dependencies:
flutter:
sdk: flutter
meta: ^1.0.5
shared_preferences: ^0.5.3+1

dev_dependencies:
test: ^1.3.0
Expand Down