Update shortcut sample
am: 8d2a8f05c7
Change-Id: I42fbdddec5a5717ab4aef4def6fc528fa8f7a432
This commit is contained in:
@@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
<uses-sdk android:minSdkVersion="25" />
|
<uses-sdk android:minSdkVersion="25" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:icon="@drawable/app"
|
android:icon="@drawable/app"
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import android.app.AlertDialog;
|
|||||||
import android.app.ListActivity;
|
import android.app.ListActivity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ShortcutInfo;
|
import android.content.pm.ShortcutInfo;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@@ -98,13 +99,27 @@ public class Main extends ListActivity implements OnClickListener {
|
|||||||
.setPositiveButton("Add", (dialog, whichButton) -> {
|
.setPositiveButton("Add", (dialog, whichButton) -> {
|
||||||
final String url = editUri.getText().toString().trim();
|
final String url = editUri.getText().toString().trim();
|
||||||
if (url.length() > 0) {
|
if (url.length() > 0) {
|
||||||
mHelper.addWebSiteShortcut(url);
|
addUriAsync(url);
|
||||||
refreshList();
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.show();
|
.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void addUriAsync(String uri) {
|
||||||
|
new AsyncTask<Void, Void, Void>() {
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params) {
|
||||||
|
mHelper.addWebSiteShortcut(uri);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void aVoid) {
|
||||||
|
refreshList();
|
||||||
|
}
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
|
||||||
private void refreshList() {
|
private void refreshList() {
|
||||||
mAdapter.setShortcuts(mHelper.getShortcuts());
|
mAdapter.setShortcuts(mHelper.getShortcuts());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,19 @@ import android.content.Context;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.ShortcutInfo;
|
import android.content.pm.ShortcutInfo;
|
||||||
import android.content.pm.ShortcutManager;
|
import android.content.pm.ShortcutManager;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.drawable.Icon;
|
import android.graphics.drawable.Icon;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.PersistableBundle;
|
import android.os.PersistableBundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLConnection;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -169,8 +176,12 @@ public class ShortcutHelper {
|
|||||||
b.setShortLabel(uri.getHost());
|
b.setShortLabel(uri.getHost());
|
||||||
b.setLongLabel(uri.toString());
|
b.setLongLabel(uri.toString());
|
||||||
|
|
||||||
// TODO Fetch the favicon from the URI and sets to the icon.
|
Bitmap bmp = fetchFavicon(uri);
|
||||||
b.setIcon(Icon.createWithResource(mContext, R.drawable.link));
|
if (bmp != null) {
|
||||||
|
b.setIcon(Icon.createWithBitmap(bmp));
|
||||||
|
} else {
|
||||||
|
b.setIcon(Icon.createWithResource(mContext, R.drawable.link));
|
||||||
|
}
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
@@ -209,4 +220,23 @@ public class ShortcutHelper {
|
|||||||
public void enableShortcut(ShortcutInfo shortcut) {
|
public void enableShortcut(ShortcutInfo shortcut) {
|
||||||
mShortcutManager.enableShortcuts(Arrays.asList(shortcut.getId()));
|
mShortcutManager.enableShortcuts(Arrays.asList(shortcut.getId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Bitmap fetchFavicon(Uri uri) {
|
||||||
|
final Uri iconUri = uri.buildUpon().path("favicon.ico").build();
|
||||||
|
Log.i(TAG, "Fetching favicon from: " + iconUri);
|
||||||
|
|
||||||
|
InputStream is = null;
|
||||||
|
BufferedInputStream bis = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
URLConnection conn = new URL(iconUri.toString()).openConnection();
|
||||||
|
conn.connect();
|
||||||
|
is = conn.getInputStream();
|
||||||
|
bis = new BufferedInputStream(is, 8192);
|
||||||
|
return BitmapFactory.decodeStream(bis);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Log.w(TAG, "Failed to fetch favicon from " + iconUri, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,20 +16,17 @@
|
|||||||
package com.example.android.shortcutsample;
|
package com.example.android.shortcutsample;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.ShortcutInfo;
|
import android.os.Handler;
|
||||||
import android.os.AsyncTask;
|
import android.os.Looper;
|
||||||
import android.os.PersistableBundle;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
private Utils() {
|
private Utils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void showToast(Context context, String message) {
|
public static void showToast(Context context, String message) {
|
||||||
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
new Handler(Looper.getMainLooper()).post(() -> {
|
||||||
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user