Bug 886627 - Add AndroidBridge directory API; r=rbarker

This commit is contained in:
Jim Chen 2014-07-24 16:42:50 -04:00
parent 070f569cd9
commit 990996bf64
5 changed files with 67 additions and 0 deletions

View File

@ -89,6 +89,7 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
@ -2541,4 +2542,38 @@ public class GeckoAppShell
return connection.getContentType();
}
/**
* Retrieve the absolute path of an external storage directory.
*
* @param type The type of directory to return
* @return Absolute path of the specified directory or null on failure
*/
@WrapElementForJNI
static String getExternalPublicDirectory(final String type) {
final String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state) &&
!Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// External storage is not available.
return null;
}
if ("sdcard".equals(type)) {
// SD card has a separate path.
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
final String systemType;
if ("downloads".equals(type)) {
systemType = Environment.DIRECTORY_DOWNLOADS;
} else if ("pictures".equals(type)) {
systemType = Environment.DIRECTORY_PICTURES;
} else if ("videos".equals(type)) {
systemType = Environment.DIRECTORY_MOVIES;
} else if ("music".equals(type)) {
systemType = Environment.DIRECTORY_MUSIC;
} else {
return null;
}
return Environment.getExternalStoragePublicDirectory(systemType).getAbsolutePath();
}
}

View File

@ -2135,3 +2135,14 @@ nsresult AndroidBridge::InputStreamRead(jobject obj, char *aBuf, uint32_t aCount
*aRead = read;
return NS_OK;
}
nsresult AndroidBridge::GetExternalPublicDirectory(const nsAString& aType, nsAString& aPath) {
AutoLocalJNIFrame frame(1);
const jstring path = GeckoAppShell::GetExternalPublicDirectory(aType);
if (!path) {
return NS_ERROR_NOT_AVAILABLE;
}
nsJNIString pathStr(path, frame.GetEnv());
aPath.Assign(pathStr);
return NS_OK;
}

View File

@ -351,6 +351,8 @@ public:
static uint32_t InputStreamAvailable(jobject obj);
static nsresult InputStreamRead(jobject obj, char *aBuf, uint32_t aCount, uint32_t *aRead);
static nsresult GetExternalPublicDirectory(const nsAString& aType, nsAString& aPath);
protected:
static StaticRefPtr<AndroidBridge> sBridge;
nsTArray<nsCOMPtr<nsIMobileMessageCallback> > mSmsRequests;

View File

@ -43,6 +43,7 @@ jmethodID GeckoAppShell::jGetCurrentNetworkInformationWrapper = 0;
jmethodID GeckoAppShell::jGetDensity = 0;
jmethodID GeckoAppShell::jGetDpiWrapper = 0;
jmethodID GeckoAppShell::jGetExtensionFromMimeTypeWrapper = 0;
jmethodID GeckoAppShell::jGetExternalPublicDirectory = 0;
jmethodID GeckoAppShell::jGetHandlersForMimeTypeWrapper = 0;
jmethodID GeckoAppShell::jGetHandlersForURLWrapper = 0;
jmethodID GeckoAppShell::jGetIconForExtensionWrapper = 0;
@ -128,6 +129,7 @@ void GeckoAppShell::InitStubs(JNIEnv *jEnv) {
jGetDensity = getStaticMethod("getDensity", "()F");
jGetDpiWrapper = getStaticMethod("getDpi", "()I");
jGetExtensionFromMimeTypeWrapper = getStaticMethod("getExtensionFromMimeType", "(Ljava/lang/String;)Ljava/lang/String;");
jGetExternalPublicDirectory = getStaticMethod("getExternalPublicDirectory", "(Ljava/lang/String;)Ljava/lang/String;");
jGetHandlersForMimeTypeWrapper = getStaticMethod("getHandlersForMimeType", "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;");
jGetHandlersForURLWrapper = getStaticMethod("getHandlersForURL", "(Ljava/lang/String;Ljava/lang/String;)[Ljava/lang/String;");
jGetIconForExtensionWrapper = getStaticMethod("getIconForExtension", "(Ljava/lang/String;I)[B");
@ -604,6 +606,21 @@ jstring GeckoAppShell::GetExtensionFromMimeTypeWrapper(const nsAString& a0) {
return ret;
}
jstring GeckoAppShell::GetExternalPublicDirectory(const nsAString& a0) {
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (env->PushLocalFrame(2) != 0) {
AndroidBridge::HandleUncaughtException(env);
MOZ_CRASH("Exception should have caused crash.");
}
jstring j0 = AndroidBridge::NewJavaString(env, a0);
jobject temp = env->CallStaticObjectMethod(mGeckoAppShellClass, jGetExternalPublicDirectory, j0);
AndroidBridge::HandleUncaughtException(env);
jstring ret = static_cast<jstring>(env->PopLocalFrame(temp));
return ret;
}
jobjectArray GeckoAppShell::GetHandlersForMimeTypeWrapper(const nsAString& a0, const nsAString& a1) {
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (env->PushLocalFrame(3) != 0) {

View File

@ -50,6 +50,7 @@ public:
static jfloat GetDensity();
static int32_t GetDpiWrapper();
static jstring GetExtensionFromMimeTypeWrapper(const nsAString& a0);
static jstring GetExternalPublicDirectory(const nsAString& a0);
static jobjectArray GetHandlersForMimeTypeWrapper(const nsAString& a0, const nsAString& a1);
static jobjectArray GetHandlersForURLWrapper(const nsAString& a0, const nsAString& a1);
static jbyteArray GetIconForExtensionWrapper(const nsAString& a0, int32_t a1);
@ -134,6 +135,7 @@ protected:
static jmethodID jGetDensity;
static jmethodID jGetDpiWrapper;
static jmethodID jGetExtensionFromMimeTypeWrapper;
static jmethodID jGetExternalPublicDirectory;
static jmethodID jGetHandlersForMimeTypeWrapper;
static jmethodID jGetHandlersForURLWrapper;
static jmethodID jGetIconForExtensionWrapper;