mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-31 14:15:30 +00:00
207 lines
7.4 KiB
Java
207 lines
7.4 KiB
Java
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#filter substitution
|
|
package @ANDROID_PACKAGE_NAME@;
|
|
|
|
import android.app.Activity;
|
|
import android.app.AlertDialog;
|
|
import android.content.Context;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
import android.os.AsyncTask;
|
|
import android.os.Bundle;
|
|
import android.text.TextUtils;
|
|
import android.util.DisplayMetrics;
|
|
import android.view.Window;
|
|
import android.widget.SimpleAdapter;
|
|
import android.widget.Toast;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
import org.json.JSONTokener;
|
|
import org.mozilla.gecko.GeckoApp;
|
|
import org.mozilla.gecko.GeckoProfile;
|
|
import org.mozilla.gecko.R;
|
|
import org.mozilla.gecko.WebAppAllocator;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
|
|
public class LauncherShortcuts extends Activity {
|
|
|
|
private ArrayList <HashMap<String, String>> mWebappsList;
|
|
private File mWebappsFolder;
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
|
setContentView(R.layout.launch_app_list);
|
|
|
|
final Intent intent = getIntent();
|
|
final String action = intent.getAction();
|
|
|
|
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
|
|
// Doing it as a background task, as it involves file access
|
|
new FetchWebApps().execute();
|
|
}
|
|
}
|
|
|
|
public void onListItemClick(int id) {
|
|
HashMap<String, String> map = mWebappsList.get(id);
|
|
|
|
String uri = map.get("uri").toString();
|
|
String title = map.get("title").toString();
|
|
String appKey = map.get("appKey").toString();
|
|
String favicon = map.get("favicon").toString();
|
|
|
|
File manifestFile = new File(mWebappsFolder, appKey + "/manifest.json");
|
|
|
|
// Parse the contents into a string
|
|
String manifestJson = new String();
|
|
try {
|
|
BufferedReader in = new BufferedReader(new FileReader(manifestFile));
|
|
String line = new String();
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
manifestJson += line;
|
|
}
|
|
} catch (IOException e) { }
|
|
|
|
try {
|
|
JSONObject manifest = (JSONObject) new JSONTokener(manifestJson).nextValue();
|
|
uri += manifest.getString("launch_path");
|
|
} catch (JSONException e) { }
|
|
|
|
WebAppAllocator allocator = WebAppAllocator.getInstance(this);
|
|
int index = allocator.findAndAllocateIndex(uri);
|
|
Intent shortcutintent = new Intent(GeckoApp.ACTION_WEBAPP_PREFIX + index);
|
|
shortcutintent.setClassName(this, GeckoApp.mAppContext.getPackageName() + ".WebApps$WebApp" + index);
|
|
shortcutintent.putExtra("args", "--webapp=" + uri);
|
|
|
|
Intent intent = new Intent();
|
|
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
|
|
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutintent);
|
|
|
|
DisplayMetrics dm = new DisplayMetrics();
|
|
getWindowManager().getDefaultDisplay().getMetrics(dm);
|
|
int size;
|
|
switch (dm.densityDpi) {
|
|
case DisplayMetrics.DENSITY_MEDIUM:
|
|
size = 48;
|
|
break;
|
|
case DisplayMetrics.DENSITY_HIGH:
|
|
size = 72;
|
|
break;
|
|
default:
|
|
size = 72;
|
|
}
|
|
|
|
Bitmap bitmap = BitmapFactory.decodeFile(favicon);
|
|
if (bitmap != null) {
|
|
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
|
|
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
|
|
}
|
|
|
|
// Now, return the result to the launcher
|
|
setResult(RESULT_OK, intent);
|
|
finish();
|
|
}
|
|
|
|
private class FetchWebApps extends AsyncTask<Void, Void, Void> {
|
|
|
|
@Override
|
|
protected Void doInBackground(Void... unused) {
|
|
mWebappsList = null;
|
|
|
|
Context context = getApplicationContext();
|
|
GeckoProfile profile = GeckoProfile.get(context);
|
|
String webappsJson = null;
|
|
try {
|
|
webappsJson = profile.readFile("webapps" + File.separatorChar + "webapps.json");
|
|
} catch (IOException ioe) {
|
|
// unable to load the file, leave webappsJson as null
|
|
}
|
|
|
|
if (TextUtils.isEmpty(webappsJson))
|
|
return null;
|
|
|
|
mWebappsList = new ArrayList<HashMap<String, String>>();
|
|
|
|
try {
|
|
JSONObject webApps = (JSONObject) new JSONTokener(webappsJson).nextValue();
|
|
|
|
@SuppressWarnings("rawtypes") Iterator appKeys = webApps.keys();
|
|
HashMap<String, String> map;
|
|
|
|
while (appKeys.hasNext()) {
|
|
String appKey = (String)appKeys.next();
|
|
JSONObject app = webApps.getJSONObject(appKey);
|
|
|
|
map = new HashMap<String, String>();
|
|
map.put("appKey", appKey);
|
|
map.put("favicon", mWebappsFolder.getPath() + "/" + appKey + "/icon.png");
|
|
map.put("title", app.getString("title"));
|
|
map.put("uri", app.getString("appURI"));
|
|
|
|
mWebappsList.add(map);
|
|
}
|
|
|
|
} catch (JSONException e) {}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
protected void onPostExecute(Void unused) {
|
|
if (mWebappsList != null) {
|
|
AlertDialog.Builder builder;
|
|
|
|
if (android.os.Build.VERSION.SDK_INT >= 11) {
|
|
builder = new AlertDialog.Builder(LauncherShortcuts.this, AlertDialog.THEME_HOLO_LIGHT);
|
|
} else {
|
|
builder = new AlertDialog.Builder(LauncherShortcuts.this);
|
|
}
|
|
|
|
builder.setTitle(R.string.launcher_shortcuts_title);
|
|
builder.setAdapter(new SimpleAdapter(
|
|
LauncherShortcuts.this,
|
|
mWebappsList,
|
|
R.layout.launch_app_listitem,
|
|
new String[] { "favicon", "title" },
|
|
new int[] { R.id.favicon, R.id.title }
|
|
), new DialogInterface.OnClickListener() {
|
|
public void onClick(DialogInterface dialog, int id) {
|
|
dialog.dismiss();
|
|
onListItemClick(id);
|
|
finish();
|
|
}
|
|
});
|
|
|
|
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
|
|
public void onCancel(DialogInterface dialog) {
|
|
dialog.dismiss();
|
|
finish();
|
|
}
|
|
});
|
|
|
|
builder.create().show();
|
|
} else {
|
|
Toast.makeText(LauncherShortcuts.this, R.string.launcher_shortcuts_empty, Toast.LENGTH_LONG).show();
|
|
finish();
|
|
}
|
|
}
|
|
}
|
|
}
|