2011-07-01 11:26:00 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* 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/. */
|
2011-07-01 11:26:00 -07:00
|
|
|
|
|
|
|
#filter substitution
|
|
|
|
package @ANDROID_PACKAGE_NAME@;
|
|
|
|
|
|
|
|
import java.io.*;
|
2011-10-04 14:45:00 -07:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import org.json.*;
|
2011-07-01 11:26:00 -07:00
|
|
|
|
|
|
|
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.*;
|
|
|
|
|
|
|
|
|
2011-10-05 13:28:00 -07:00
|
|
|
public class LauncherShortcuts extends Activity {
|
2011-07-01 11:26:00 -07:00
|
|
|
|
2011-10-04 14:45:00 -07:00
|
|
|
private ArrayList <HashMap<String, String>> mWebappsList;
|
|
|
|
private File mWebappsFolder;
|
2011-07-01 11:26:00 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
2011-10-05 13:28:00 -07:00
|
|
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
2011-07-01 11:26:00 -07:00
|
|
|
setContentView(R.layout.launch_app_list);
|
|
|
|
|
|
|
|
final Intent intent = getIntent();
|
|
|
|
final String action = intent.getAction();
|
|
|
|
|
|
|
|
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
|
2011-10-04 14:45:00 -07:00
|
|
|
// Doing it as a background task, as it involves file access
|
|
|
|
new FetchWebApps().execute();
|
2011-07-01 11:26:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-05 13:28:00 -07:00
|
|
|
public void onListItemClick(int id) {
|
|
|
|
HashMap<String, String> map = mWebappsList.get(id);
|
2011-10-04 14:45:00 -07:00
|
|
|
|
|
|
|
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) { }
|
|
|
|
|
2011-07-01 11:26:00 -07:00
|
|
|
Intent shortcutintent = new Intent("org.mozilla.gecko.WEBAPP");
|
|
|
|
shortcutintent.setClass(this, App.class);
|
2011-10-04 14:45:00 -07:00
|
|
|
shortcutintent.putExtra("args", "--webapp=" + uri);
|
2011-07-01 11:26:00 -07:00
|
|
|
|
|
|
|
Intent intent = new Intent();
|
2011-10-04 14:45:00 -07:00
|
|
|
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
|
2011-07-01 11:26:00 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-05 14:21:00 -07:00
|
|
|
Bitmap bitmap = BitmapFactory.decodeFile(favicon);
|
|
|
|
if (bitmap != null) {
|
|
|
|
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, size, size, true);
|
|
|
|
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
|
|
|
|
}
|
2011-07-01 11:26:00 -07:00
|
|
|
|
|
|
|
// Now, return the result to the launcher
|
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
finish();
|
|
|
|
}
|
2011-10-04 14:45:00 -07:00
|
|
|
|
|
|
|
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) {
|
2011-10-05 13:28:00 -07:00
|
|
|
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(
|
2011-10-04 14:45:00 -07:00
|
|
|
LauncherShortcuts.this,
|
|
|
|
mWebappsList,
|
|
|
|
R.layout.launch_app_listitem,
|
|
|
|
new String[] { "favicon", "title" },
|
|
|
|
new int[] { R.id.favicon, R.id.title }
|
2011-10-05 13:28:00 -07:00
|
|
|
), 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();
|
2011-10-04 14:45:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-01 11:26:00 -07:00
|
|
|
}
|