2013-02-01 23:45:33 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* 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/.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2013-03-15 10:52:53 +00:00
|
|
|
import org.mozilla.gecko.util.ThreadUtils;
|
2013-02-01 23:45:33 +00:00
|
|
|
|
2013-04-08 07:51:56 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
2013-02-01 23:45:33 +00:00
|
|
|
import android.app.Activity;
|
2013-02-14 23:35:39 +00:00
|
|
|
import android.content.Context;
|
2013-02-01 23:45:33 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2013-02-14 23:35:39 +00:00
|
|
|
import java.io.BufferedReader;
|
2013-02-01 23:45:33 +00:00
|
|
|
import java.io.File;
|
2013-02-14 23:35:39 +00:00
|
|
|
import java.io.FileInputStream;
|
2013-02-01 23:45:33 +00:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2013-02-14 23:35:39 +00:00
|
|
|
import java.io.InputStreamReader;
|
2013-02-01 23:45:33 +00:00
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.Enumeration;
|
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
import java.util.zip.ZipFile;
|
|
|
|
|
|
|
|
public final class Distribution {
|
2013-02-01 23:45:33 +00:00
|
|
|
private static final String LOGTAG = "GeckoDistribution";
|
|
|
|
|
2013-02-14 03:25:38 +00:00
|
|
|
private static final int STATE_UNKNOWN = 0;
|
|
|
|
private static final int STATE_NONE = 1;
|
|
|
|
private static final int STATE_SET = 2;
|
2013-02-01 23:45:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes distribution if it hasn't already been initalized.
|
2013-02-19 18:15:55 +00:00
|
|
|
*
|
|
|
|
* @param packagePath specifies where to look for the distribution directory.
|
2013-02-01 23:45:33 +00:00
|
|
|
*/
|
2013-02-19 18:15:55 +00:00
|
|
|
public static void init(final Context context, final String packagePath) {
|
2013-02-01 23:45:33 +00:00
|
|
|
// Read/write preferences and files on the background thread.
|
2013-03-15 10:52:53 +00:00
|
|
|
ThreadUtils.postToBackgroundThread(new Runnable() {
|
2013-02-27 05:48:00 +00:00
|
|
|
@Override
|
2013-02-01 23:45:33 +00:00
|
|
|
public void run() {
|
|
|
|
// Bail if we've already initialized the distribution.
|
2013-02-14 23:35:39 +00:00
|
|
|
SharedPreferences settings = context.getSharedPreferences(GeckoApp.PREFS_NAME, Activity.MODE_PRIVATE);
|
|
|
|
String keyName = context.getPackageName() + ".distribution_state";
|
2013-02-01 23:45:33 +00:00
|
|
|
int state = settings.getInt(keyName, STATE_UNKNOWN);
|
2013-02-19 18:49:42 +00:00
|
|
|
if (state == STATE_NONE) {
|
2013-02-01 23:45:33 +00:00
|
|
|
return;
|
2013-02-19 18:15:55 +00:00
|
|
|
}
|
2013-02-01 23:45:33 +00:00
|
|
|
|
2013-02-01 23:45:33 +00:00
|
|
|
// Send a message to Gecko if we've set a distribution.
|
|
|
|
if (state == STATE_SET) {
|
2013-07-05 14:37:00 +00:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Distribution:Set", ""));
|
2013-02-01 23:45:33 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-02-01 23:45:33 +00:00
|
|
|
|
2013-02-01 23:45:33 +00:00
|
|
|
boolean distributionSet = false;
|
2013-02-01 23:45:33 +00:00
|
|
|
try {
|
2013-02-25 04:15:23 +00:00
|
|
|
// First, try copying distribution files out of the APK.
|
2013-02-19 18:15:55 +00:00
|
|
|
distributionSet = copyFiles(context, packagePath);
|
2013-02-01 23:45:33 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(LOGTAG, "Error copying distribution files", e);
|
|
|
|
}
|
2013-02-01 23:45:33 +00:00
|
|
|
|
2013-02-25 04:15:23 +00:00
|
|
|
if (!distributionSet) {
|
|
|
|
// If there aren't any distribution files in the APK, look in the /system directory.
|
|
|
|
File distDir = new File("/system/" + context.getPackageName() + "/distribution");
|
|
|
|
if (distDir.exists()) {
|
|
|
|
distributionSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 23:45:33 +00:00
|
|
|
if (distributionSet) {
|
2013-07-05 14:37:00 +00:00
|
|
|
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Distribution:Set", ""));
|
2013-02-01 23:45:33 +00:00
|
|
|
settings.edit().putInt(keyName, STATE_SET).commit();
|
|
|
|
} else {
|
|
|
|
settings.edit().putInt(keyName, STATE_NONE).commit();
|
|
|
|
}
|
2013-02-01 23:45:33 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copies the /distribution folder out of the APK and into the app's data directory.
|
2013-02-01 23:45:33 +00:00
|
|
|
* Returns true if distribution files were found and copied.
|
2013-02-01 23:45:33 +00:00
|
|
|
*/
|
2013-02-19 18:15:55 +00:00
|
|
|
private static boolean copyFiles(Context context, String packagePath) throws IOException {
|
|
|
|
File applicationPackage = new File(packagePath);
|
2013-02-01 23:45:33 +00:00
|
|
|
ZipFile zip = new ZipFile(applicationPackage);
|
|
|
|
|
2013-02-01 23:45:33 +00:00
|
|
|
boolean distributionSet = false;
|
2013-02-01 23:45:33 +00:00
|
|
|
Enumeration<? extends ZipEntry> zipEntries = zip.entries();
|
|
|
|
while (zipEntries.hasMoreElements()) {
|
|
|
|
ZipEntry fileEntry = zipEntries.nextElement();
|
|
|
|
String name = fileEntry.getName();
|
|
|
|
|
|
|
|
if (!name.startsWith("distribution/"))
|
|
|
|
continue;
|
|
|
|
|
2013-02-01 23:45:33 +00:00
|
|
|
distributionSet = true;
|
|
|
|
|
2013-02-14 23:35:39 +00:00
|
|
|
File dataDir = new File(context.getApplicationInfo().dataDir);
|
2013-02-01 23:45:33 +00:00
|
|
|
File outFile = new File(dataDir, name);
|
|
|
|
|
|
|
|
File dir = outFile.getParentFile();
|
|
|
|
if (!dir.exists())
|
|
|
|
dir.mkdirs();
|
|
|
|
|
|
|
|
InputStream fileStream = zip.getInputStream(fileEntry);
|
|
|
|
OutputStream outStream = new FileOutputStream(outFile);
|
|
|
|
|
|
|
|
int b;
|
|
|
|
while ((b = fileStream.read()) != -1)
|
|
|
|
outStream.write(b);
|
|
|
|
|
|
|
|
fileStream.close();
|
|
|
|
outStream.close();
|
|
|
|
outFile.setLastModified(fileEntry.getTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
zip.close();
|
2013-02-01 23:45:33 +00:00
|
|
|
|
|
|
|
return distributionSet;
|
2013-02-01 23:45:33 +00:00
|
|
|
}
|
2013-02-14 23:35:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns parsed contents of bookmarks.json.
|
|
|
|
* This method should only be called from a background thread.
|
|
|
|
*/
|
|
|
|
public static JSONArray getBookmarks(Context context) {
|
|
|
|
SharedPreferences settings = context.getSharedPreferences(GeckoApp.PREFS_NAME, Activity.MODE_PRIVATE);
|
|
|
|
String keyName = context.getPackageName() + ".distribution_state";
|
|
|
|
int state = settings.getInt(keyName, STATE_UNKNOWN);
|
|
|
|
if (state == STATE_NONE) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZipFile zip = null;
|
|
|
|
InputStream inputStream = null;
|
|
|
|
try {
|
|
|
|
if (state == STATE_UNKNOWN) {
|
2013-05-15 22:31:27 +00:00
|
|
|
// If the distribution hasn't been set yet, first look for bookmarks.json in the APK.
|
2013-02-14 23:35:39 +00:00
|
|
|
File applicationPackage = new File(context.getPackageResourcePath());
|
|
|
|
zip = new ZipFile(applicationPackage);
|
|
|
|
ZipEntry zipEntry = zip.getEntry("distribution/bookmarks.json");
|
2013-05-15 22:31:27 +00:00
|
|
|
if (zipEntry != null) {
|
|
|
|
inputStream = zip.getInputStream(zipEntry);
|
|
|
|
} else {
|
|
|
|
// If there's no bookmarks.json in the APK, but there is a preferences.json,
|
|
|
|
// don't create any distribution bookmarks.
|
|
|
|
zipEntry = zip.getEntry("distribution/preferences.json");
|
|
|
|
if (zipEntry != null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// Otherwise, look for bookmarks.json in the /system directory.
|
|
|
|
File systemFile = new File("/system/" + context.getPackageName() + "/distribution/bookmarks.json");
|
|
|
|
if (!systemFile.exists()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
inputStream = new FileInputStream(systemFile);
|
2013-02-14 23:35:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-07-03 04:40:47 +00:00
|
|
|
// Otherwise, first look for the distribution in the data directory.
|
|
|
|
File distDir = new File(context.getApplicationInfo().dataDir, "distribution");
|
|
|
|
if (!distDir.exists()) {
|
|
|
|
// If that doesn't exist, then we must be using a distribution from the system directory.
|
|
|
|
distDir = new File("/system/" + context.getPackageName() + "/distribution");
|
2013-05-15 22:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
File file = new File(distDir, "bookmarks.json");
|
2013-02-14 23:35:39 +00:00
|
|
|
inputStream = new FileInputStream(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert input stream to JSONArray
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
String s;
|
|
|
|
while ((s = reader.readLine()) != null) {
|
|
|
|
stringBuilder.append(s);
|
|
|
|
}
|
|
|
|
return new JSONArray(stringBuilder.toString());
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(LOGTAG, "Error getting bookmarks", e);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "Error parsing bookmarks.json", e);
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (zip != null) {
|
|
|
|
zip.close();
|
|
|
|
}
|
|
|
|
if (inputStream != null) {
|
|
|
|
inputStream.close();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(LOGTAG, "Error closing streams", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-02-01 23:45:33 +00:00
|
|
|
}
|