Android life cycle improvements and Gradle integration

This commit is contained in:
Imanol Fernandez 2017-02-28 14:17:59 +01:00
parent 350d9c6c47
commit 7a2a90959e
30 changed files with 945 additions and 634 deletions

View file

@ -27,3 +27,6 @@ env_logger = "0.4"
[target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))'.dependencies]
xdg = "2.0"
[target.'cfg(target_os = "android")'.dependencies]
android_injected_glue = {git = "https://github.com/mmatyas/android-rs-injected-glue"}

View file

@ -6,8 +6,12 @@
//! For linux based platforms, it uses the XDG base directory spec but provides
//! similar abstractions for non-linux platforms.
#[cfg(target_os = "android")]
use android_injected_glue;
#[cfg(any(target_os = "macos", target_os = "windows"))]
use std::env;
#[cfg(target_os = "android")]
use std::ffi::CStr;
use std::path::PathBuf;
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
use xdg;
@ -20,8 +24,12 @@ pub fn default_config_dir() -> Option<PathBuf> {
}
#[cfg(target_os = "android")]
#[allow(unsafe_code)]
pub fn default_config_dir() -> Option<PathBuf> {
Some(PathBuf::from("/sdcard/servo"))
let dir = unsafe {
CStr::from_ptr((*android_injected_glue::get_app().activity).externalDataPath)
};
Some(PathBuf::from(dir.to_str().unwrap()))
}
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
@ -32,8 +40,12 @@ pub fn default_data_dir() -> Option<PathBuf> {
}
#[cfg(target_os = "android")]
#[allow(unsafe_code)]
pub fn default_data_dir() -> Option<PathBuf> {
Some(PathBuf::from("/sdcard/servo"))
let dir = unsafe {
CStr::from_ptr((*android_injected_glue::get_app().activity).internalDataPath)
};
Some(PathBuf::from(dir.to_str().unwrap()))
}
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))]
@ -44,8 +56,14 @@ pub fn default_cache_dir() -> Option<PathBuf> {
}
#[cfg(target_os = "android")]
#[allow(unsafe_code)]
pub fn default_cache_dir() -> Option<PathBuf> {
Some(PathBuf::from("/sdcard/servo"))
// TODO: Use JNI to call context.getCacheDir().
// There is no equivalent function in NDK/NativeActivity.
let dir = unsafe {
CStr::from_ptr((*android_injected_glue::get_app().activity).externalDataPath)
};
Some(PathBuf::from(dir.to_str().unwrap()))
}
#[cfg(target_os = "macos")]

View file

@ -4,6 +4,8 @@
#![deny(unsafe_code)]
#[cfg(target_os = "android")]
extern crate android_injected_glue;
extern crate euclid;
extern crate getopts;
#[macro_use] extern crate lazy_static;

View file

@ -2,8 +2,12 @@
* 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/. */
#[cfg(target_os = "android")]
use android_injected_glue;
#[cfg(not(target_os = "android"))]
use std::env;
#[cfg(target_os = "android")]
use std::ffi::CStr;
use std::fs::File;
use std::io::{self, Read};
use std::path::{Path, PathBuf};
@ -21,8 +25,12 @@ pub fn set_resources_path(path: Option<String>) {
}
#[cfg(target_os = "android")]
#[allow(unsafe_code)]
pub fn resources_dir_path() -> io::Result<PathBuf> {
Ok(PathBuf::from("/sdcard/servo/"))
let dir = unsafe {
CStr::from_ptr((*android_injected_glue::get_app().activity).externalDataPath)
};
Ok(PathBuf::from(dir.to_str().unwrap()))
}
#[cfg(not(target_os = "android"))]