blob: 68edb82ae14bc6fc61c23e98502aeb7082da47d8 [file] [log] [blame]
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.pm.shortcuts;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.content.pm.LauncherApps;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.os.Process;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class Main extends Activity {
public static final String TAG = "ShortcutDemo";
private static final boolean USE_LAUNCHER_APIS = true;
private ShortcutManager mShortcutManager;
private LauncherApps mLauncherApps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mShortcutManager = getSystemService(ShortcutManager.class);
mLauncherApps = getSystemService(LauncherApps.class);
// TODO This will break once LauncherApps implements permission checks.
if (USE_LAUNCHER_APIS) {
mLauncherApps.registerCallback(mLauncherCallback);
}
WallpaperManager wpm = this.getSystemService(WallpaperManager.class);
wpm.getWallpaperFile(WallpaperManager.FLAG_SET_LOCK);
}
@Override
protected void onDestroy() {
if (USE_LAUNCHER_APIS) {
mLauncherApps.unregisterCallback(mLauncherCallback);
}
super.onDestroy();
}
private void dumpCurrentShortcuts() {
Log.d(TAG, "Dynamic shortcuts:");
for (ShortcutInfo si : mShortcutManager.getDynamicShortcuts()) {
Log.d(TAG, " " + si.toString());
}
Log.d(TAG, "Pinned shortcuts:");
for (ShortcutInfo si : mShortcutManager.getPinnedShortcuts()) {
Log.d(TAG, " " + si.toString());
}
}
private void showThrottledToast() {
Toast.makeText(this,
"Throttled, use \"adb shell cmd shortcut reset-throttling\" to reset counters",
Toast.LENGTH_SHORT).show();
}
public void onPublishPressed(View view) {
dumpCurrentShortcuts();
final Icon icon1 = Icon.createWithResource(this, R.drawable.icon_large_1);
final Icon icon2 = Icon.createWithBitmap(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_large_2));
final Icon icon3 = Icon.createWithContentUri(
Uri.parse("content://com.example.android.pm.shortcuts/" + R.drawable.icon_large_3));
final Intent intent1 = new Intent(Intent.ACTION_VIEW);
intent1.setClass(this, Main.class);
intent1.putExtra("str", "str-value");
intent1.putExtra("nest", new Bundle());
intent1.getBundleExtra("nest").putInt("int", 123);
final Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setClass(this, Main.class);
intent2.putExtra("str", "2");
final Intent intent3 = new Intent(Intent.ACTION_VIEW);
intent2.setClass(this, Main.class);
final ShortcutInfo si1 = new ShortcutInfo.Builder(this)
.setId("shortcut1")
.setTitle("Title 1")
.setIcon(icon1)
.setWeight(10)
.setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")))
.build();
final ShortcutInfo si2 = new ShortcutInfo.Builder(this)
.setId("shortcut2")
.setTitle("Title 2")
.setIcon(icon2)
.setWeight(5)
.setIntent(intent2)
.build();
final ShortcutInfo si3 = new ShortcutInfo.Builder(this)
.setId("shortcut3")
.setTitle("Title 3")
.setIcon(icon3)
.setWeight(15)
.setIntent(intent3)
.build();
if (!mShortcutManager.setDynamicShortcuts(Arrays.asList(si1, si2, si3))) {
showThrottledToast();
}
mLauncherApps.startShortcut(this.getPackageName(), "shortcut1", null, null, Process.myUserHandle());
}
private final LauncherApps.Callback mLauncherCallback = new LauncherApps.Callback() {
@Override
public void onPackageRemoved(String packageName, UserHandle user) {
}
@Override
public void onPackageAdded(String packageName, UserHandle user) {
}
@Override
public void onPackageChanged(String packageName, UserHandle user) {
}
@Override
public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) {
}
@Override
public void onPackagesUnavailable(String[] packageNames, UserHandle user,
boolean replacing) {
}
@Override
public void onShortcutsChanged(String packageName,
List<ShortcutInfo> shortcuts, UserHandle user) {
Log.w(TAG, "onShortcutsChanged: user=" + user + " package=" + packageName);
Log.d(TAG, "Updated shortcuts:");
for (ShortcutInfo si : shortcuts) {
Log.d(TAG, " " + si.toString());
writeIconToFile(si);
}
}
};
private void writeIconToFile(ShortcutInfo si) {
if (!si.hasIconFile()){
return;
}
String filename = Environment.getExternalStorageDirectory() + "/" + si.getId() + ".png";
try (
ParcelFileDescriptor pfd = mLauncherApps.getShortcutIconFd(si,
Process.myUserHandle());
FileInputStream in = new FileInputStream(pfd.getFileDescriptor());
FileOutputStream out = new FileOutputStream(filename)) {
byte[] buf = new byte[32 * 1024];
int len;
while ((len = in.read(buf)) >= 0) {
out.write(buf, 0, len);
}
Log.d(TAG, "wrote icon to " + filename);
} catch (IOException e) {
e.printStackTrace();
}
}
}