gecko/embedding/android/LauncherShortcuts.java.in
2012-05-21 12:12:37 +01:00

228 lines
7.8 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 java.io.*;
import java.util.*;
import org.json.*;
import org.mozilla.gecko.*;
import android.os.*;
import android.content.*;
import android.app.*;
import android.text.*;
import android.util.*;
import android.widget.*;
import android.database.sqlite.*;
import android.database.*;
import android.view.*;
import android.net.Uri;
import android.graphics.*;
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) { }
Intent shortcutintent = new Intent("org.mozilla.gecko.WEBAPP");
shortcutintent.setClass(this, App.class);
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();
File home = new File(context.getFilesDir(), "mozilla");
if (!home.exists())
home = new File(context.getExternalFilesDir(null).getPath(), "mozilla");
if (!home.exists())
return null;
File profile = null;
String[] files = home.list();
for (String file : files) {
if (file.endsWith(".default")) {
profile = new File(home, file);
break;
}
}
if (profile == null)
return null;
// Save the folder path to be used during click event
mWebappsFolder = new File(profile, "webapps");
if (!mWebappsFolder.exists())
return null;
File webapps = new File(mWebappsFolder, "webapps.json");
if (!webapps.exists())
return null;
// Parse the contents into a string
String webappsJson = new String();
try {
BufferedReader in = new BufferedReader(new FileReader(webapps));
String line = new String();
while ((line = in.readLine()) != null) {
webappsJson += line;
}
} catch (IOException e) { }
if (webappsJson.length() == 0)
return null;
mWebappsList = new ArrayList<HashMap<String, String>>();
try {
JSONObject webApps = (JSONObject) new JSONTokener(webappsJson).nextValue();
Iterator<Object> appKeys = webApps.keys();
HashMap<String, String> map;
while (appKeys.hasNext()) {
String appKey = appKeys.next().toString();
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() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
onListItemClick(id);
finish();
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
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();
}
}
}
}