gecko/embedding/android/LauncherShortcuts.java.in

236 lines
8.2 KiB
Java

/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Android code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Vladimir Vukicevic <vladimir@pobox.com>
* Wes Johnston <wjohnston@mozilla.com>
* Mark Finkle <mfinkle@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#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 ListActivity {
private ArrayList <HashMap<String, String>> mWebappsList;
private File mWebappsFolder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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();
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
HashMap<String, String> map = mWebappsList.get((int) 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) {
TextView emptyText = (TextView)findViewById(android.R.id.empty);
emptyText.setText("");
setListAdapter(new SimpleAdapter(
LauncherShortcuts.this,
mWebappsList,
R.layout.launch_app_listitem,
new String[] { "favicon", "title" },
new int[] { R.id.favicon, R.id.title }
));
}
}
}
}