Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.tmp
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ Note: This plugin is for phonegap 3.x
</script>
```

If you want to create a custom icon you can pass an object
```
<script>
window.plugins.Shortcut.CreateShortcut({
text: "Text to show"
icon: "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADi .... BgCAYAAADi" // base64 String
}, successfunc, failfunc );
</script>
```

[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/jorgecis/shortcutplugin/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.plugins.shortcut"
version="0.1.1">
version="0.0.2">

<name>Home Shortcuts</name>

Expand Down
119 changes: 87 additions & 32 deletions src/android/ShortcutPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,124 @@
import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Base64;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.*;
import android.content.Intent;
import android.content.Context;
import android.os.Parcelable;
import android.os.Parcelable;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager;

public class ShortcutPlugin extends CordovaPlugin {
public static final String ACTION_ADD_SHORTCUT = "addShortcut";
public static final String ACTION_DEL_SHORTCUT = "delShortcut";
public static final String ACTION_ADD_SHORTCUT = "addShortcut";
public static final String ACTION_DEL_SHORTCUT = "delShortcut";

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
public boolean execute(String action, JSONArray args,
CallbackContext callbackContext) throws JSONException {

try {
if (ACTION_ADD_SHORTCUT.equals(action)) {

//Get params
// Get params
JSONObject arg_object = args.getJSONObject(0);

Context context=this.cordova.getActivity().getApplicationContext();
PackageManager pm = context.getPackageManager();
// set param defaults
String shortcuttext = arg_object.getString("text");
String iconBase64 = null;
String activityClass = null;
String activityPackage = null;
String extraSubject = null;

Intent i = new Intent();
i.setClassName(this.cordova.getActivity().getPackageName(), this.cordova.getActivity().getClass().getName());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (arg_object.has("icon")) {
iconBase64 = arg_object.getString("icon");
}

Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, arg_object.getString("shortcuttext"));
if (arg_object.has("activityClass") & arg_object.has("activityPackage")) {
activityClass = arg_object.getString("activityClass");
activityPackage = arg_object.getString("activityPackage");
}

//Get Icon
ResolveInfo ri = pm.resolveActivity(i, 0);
int iconId = ri.activityInfo.applicationInfo.icon;
Parcelable icon = Intent.ShortcutIconResource.fromContext(context, iconId);
if (arg_object.has("extraSubject")) {
extraSubject = arg_object.getString("extraSubject");
}

shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
context.sendBroadcast(shortcutintent);
Context context = this.cordova.getActivity()
.getApplicationContext();
PackageManager pm = context.getPackageManager();

Intent i = new Intent();
if (activityClass == null) {
i.setClassName(this.cordova.getActivity().getPackageName(),
this.cordova.getActivity().getClass().getName());
} else {
i.setClassName(activityPackage, activityClass);
}

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

if(extraSubject != null){
i.putExtra(Intent.EXTRA_SUBJECT, extraSubject);
}

Intent shortcutintent = new Intent(
"com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
shortcuttext);

// Get Icon
if (iconBase64 == null) {
ResolveInfo ri = pm.resolveActivity(i, 0);
int iconId = ri.activityInfo.applicationInfo.icon;
Parcelable icon = Intent.ShortcutIconResource.fromContext(
context, iconId);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
} else {
//Bitmap bmpIcon = decodeBase64(iconBase64);
//Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmpIcon, 128, 128, true);
//shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON, decodeBase64(iconBase64));
}

shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
context.sendBroadcast(shortcutintent);

callbackContext.success();
return true;
} else if (ACTION_DEL_SHORTCUT.equals(action)) {
JSONObject arg_object = args.getJSONObject(0);
Context context=this.cordova.getActivity().getApplicationContext();
Context context = this.cordova.getActivity()
.getApplicationContext();

Intent i = new Intent();
i.setClassName(this.cordova.getActivity().getPackageName(), this.cordova.getActivity().getClass().getName());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent i = new Intent();
i.setClassName(this.cordova.getActivity().getPackageName(),
this.cordova.getActivity().getClass().getName());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Intent shortcutintent = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, arg_object.getString("shortcuttext"));
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
Intent shortcutintent = new Intent(
"com.android.launcher.action.UNINSTALL_SHORTCUT");
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME,
arg_object.getString("shortcuttext"));
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
context.sendBroadcast(shortcutintent);
callbackContext.success();
}
}
callbackContext.error("Invalid action");
return false;
} catch(Exception e) {
} catch (Exception e) {
System.err.println("Exception: " + e.getMessage());
callbackContext.error(e.getMessage());
return false;
}
}
}

private static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}
60 changes: 36 additions & 24 deletions www/ShortcutPlugin.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
//Copyright 2013 Jorge Cisneros [email protected]

var ShortcutPlugin = function() {};
var ShortcutPlugin = function () {
};

ShortcutPlugin.prototype.CreateShortcut = function (data, successCallback, errorCallback) {

// to provide backwards compatibility
if (typeof data === 'string') {
data = {
text: data
};
}

if (typeof data !== 'object' || typeof data.text !== 'string') {
errorCallback('required text is not set or not a string');
return;
}

ShortcutPlugin.prototype.CreateShortcut = function (shortcut_text, successCallback, errorCallback) {
cordova.exec(
successCallback,
errorCallback,
'ShortcutPlugin',
'addShortcut',
[{
"shortcuttext": shortcut_text
}]
);
cordova.exec(
successCallback,
errorCallback,
'ShortcutPlugin',
'addShortcut',
[data]
);
};
ShortcutPlugin.prototype.RemoveShortcut = function(shortcut_text, successCallback, errorCallback) {
cordova.exec(
successCallback,
errorCallback,
'ShortcutPlugin',
'delShortcut',
[{
"shortcuttext": shortcut_text
}]
);
ShortcutPlugin.prototype.RemoveShortcut = function (shortcut_text, successCallback, errorCallback) {
cordova.exec(
successCallback,
errorCallback,
'ShortcutPlugin',
'delShortcut',
[{
"shortcuttext": shortcut_text
}]
);
};

if(!window.plugins) {
window.plugins = {};
if (!window.plugins) {
window.plugins = {};
}
if (!window.plugins.Shortcut) {
window.plugins.Shortcut = new ShortcutPlugin();
window.plugins.Shortcut = new ShortcutPlugin();
}