This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[quick_actions] Support v2 android embedder. #2167
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
08319ab
migrate
87d3346
migrate
dbfbcf4
rename activities
7bed9d8
add exported=true
14ad8b1
Update MethodCallHandlerImpl.java
46a0510
Update MainActivity.java
c548b91
Update MethodCallHandlerImpl.java
33a23ce
Update MainActivity.java
9a4ca1a
fix gradle
3a29150
Update packages/quick_actions/android/src/main/java/io/flutter/plugin…
985e2ab
review fixes
a169a67
Update MainActivity.java
7b7a16e
init tests
e44e532
merge master and versioning
29077c5
merge master
25453dc
fix conflicts
ec1da8a
remove author field
a3ffc23
move back author field
92793db
fix CI warning
0d9cbfc
Merge branch 'master' into migrate_quickactions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
..._actions/android/src/main/java/io/flutter/plugins/quickactions/MethodCallHandlerImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright 2019 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugins.quickactions; | ||
|
||
import android.annotation.TargetApi; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.ShortcutInfo; | ||
import android.content.pm.ShortcutManager; | ||
import android.content.res.Resources; | ||
import android.graphics.drawable.Icon; | ||
import android.os.Build; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler { | ||
|
||
private static final String CHANNEL_ID = "plugins.flutter.io/quick_actions"; | ||
private static final String EXTRA_ACTION = "some unique action key"; | ||
|
||
private final Context context; | ||
private Activity activity; | ||
|
||
MethodCallHandlerImpl(Context context, Activity activity) { | ||
this.context = context; | ||
this.activity = activity; | ||
} | ||
|
||
void setActivity(Activity activity) { | ||
this.activity = activity; | ||
} | ||
|
||
@Override | ||
public void onMethodCall(MethodCall call, MethodChannel.Result result) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) { | ||
// We already know that this functionality does not work for anything | ||
// lower than API 25 so we chose not to return error. Instead we do nothing. | ||
result.success(null); | ||
return; | ||
} | ||
ShortcutManager shortcutManager = | ||
(ShortcutManager) context.getSystemService(Context.SHORTCUT_SERVICE); | ||
switch (call.method) { | ||
case "setShortcutItems": | ||
List<Map<String, String>> serializedShortcuts = call.arguments(); | ||
List<ShortcutInfo> shortcuts = deserializeShortcuts(serializedShortcuts); | ||
shortcutManager.setDynamicShortcuts(shortcuts); | ||
break; | ||
case "clearShortcutItems": | ||
shortcutManager.removeAllDynamicShortcuts(); | ||
break; | ||
case "getLaunchAction": | ||
if (activity == null) { | ||
result.error( | ||
"quick_action_getlaunchaction_no_activity", | ||
"There is no activity available when launching action", | ||
null); | ||
return; | ||
} | ||
final Intent intent = activity.getIntent(); | ||
final String launchAction = intent.getStringExtra(EXTRA_ACTION); | ||
if (launchAction != null && !launchAction.isEmpty()) { | ||
shortcutManager.reportShortcutUsed(launchAction); | ||
intent.removeExtra(EXTRA_ACTION); | ||
} | ||
result.success(launchAction); | ||
return; | ||
default: | ||
result.notImplemented(); | ||
return; | ||
} | ||
result.success(null); | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.N_MR1) | ||
private List<ShortcutInfo> deserializeShortcuts(List<Map<String, String>> shortcuts) { | ||
final List<ShortcutInfo> shortcutInfos = new ArrayList<>(); | ||
|
||
for (Map<String, String> shortcut : shortcuts) { | ||
final String icon = shortcut.get("icon"); | ||
final String type = shortcut.get("type"); | ||
final String title = shortcut.get("localizedTitle"); | ||
final ShortcutInfo.Builder shortcutBuilder = new ShortcutInfo.Builder(context, type); | ||
|
||
final int resourceId = loadResourceId(context, icon); | ||
final Intent intent = getIntentToOpenMainActivity(type); | ||
|
||
if (resourceId > 0) { | ||
shortcutBuilder.setIcon(Icon.createWithResource(context, resourceId)); | ||
} | ||
|
||
final ShortcutInfo shortcutInfo = | ||
shortcutBuilder.setLongLabel(title).setShortLabel(title).setIntent(intent).build(); | ||
shortcutInfos.add(shortcutInfo); | ||
} | ||
return shortcutInfos; | ||
} | ||
|
||
private int loadResourceId(Context context, String icon) { | ||
if (icon == null) { | ||
return 0; | ||
} | ||
final String packageName = context.getPackageName(); | ||
final Resources res = context.getResources(); | ||
final int resourceId = res.getIdentifier(icon, "drawable", packageName); | ||
|
||
if (resourceId == 0) { | ||
return res.getIdentifier(icon, "mipmap", packageName); | ||
} else { | ||
return resourceId; | ||
} | ||
} | ||
|
||
private Intent getIntentToOpenMainActivity(String type) { | ||
final String packageName = context.getPackageName(); | ||
|
||
return context | ||
.getPackageManager() | ||
.getLaunchIntentForPackage(packageName) | ||
.setAction(Intent.ACTION_RUN) | ||
.putExtra(EXTRA_ACTION, type) | ||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should "some unique action key" be written here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have enough context to give input here. It seems this is introduced in 63609c9
and @collinjackson or @BugsBunnyBR might be able to give some inputs on this.
Anyways, this is not related to this migration. I suggest we create new issue and fix it in a later PR if needs to be fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is just a unique string, the value itself does not matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BugsBunnyBR We can make the value of the String unambiguous so it is more readable.