I'm developing a cordava plugin that manages my expansion files for android. The reading of an expansion file works, but I have problems with downloading them.
Here is my plugin:
public class ExpansionFileReader extends CordovaPlugin implements IDownloaderClient {
private final String MEDIA_FOLDER_NAME = "mediafiles";
private final String MAIN_EXPANSION = "main_expansion";
private final String URL = "url";
private final String TAG = "expansionFileReader";
private Context context;
private CallbackContext callbackContext;
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
}
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
JSONObject callbackValue = new JSONObject();
context = this.cordova.getActivity().getApplicationContext();
if(action.equalsIgnoreCase("downloadExpansionFileIfNecessary")) {
this.callbackContext = callbackContext;
downloadExpansionFileIfNecessary();
} else if(action.equalsIgnoreCase("getFile")){
[getFileAction]
}
return true;
}
private void downloadExpansionFileIfNecessary() {
if (!expansionFilesDelivered()) {
Log.d(TAG, "NO EXPANSION FILE FOUND");
try {
Intent launchIntent = this.cordova.getActivity().getIntent();
Intent intentToLaunchThisActivityFromNotification = new Intent((context), context.getClass());
intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());
if (launchIntent.getCategories() != null) {
for (String category : launchIntent.getCategories()) {
intentToLaunchThisActivityFromNotification.addCategory(category);
}
}
PendingIntent pendingIntent = PendingIntent.getActivity((context), 0, intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);
// Request to start the download
Log.d(TAG, "REQUEST TO START DOWNLOAD");
int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(context, pendingIntent, DownloaderService.class);
if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, DownloaderService.class);
if (null != mDownloaderClientStub) {
mDownloaderClientStub.connect(context);
}
return;
}
else {
Log.d(TAG, "DOWNLOAD NOT NECESSARY");
callbackContext.success();
}
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "Cannot find package!", e);
}
} else {
validateXAPKZipFiles();
}
}
/////////////////////////////
// INIT DOWNLOADS
/////////////////////////////
private IDownloaderService mRemoteService;
private IStub mDownloaderClientStub;
private int mState;
private boolean mCancelValidation;
// region Expansion Downloader
private static class XAPKFile {
public final boolean mIsMain;
public final int mFileVersion;
public final long mFileSize;
XAPKFile(boolean isMain, int fileVersion, long fileSize) {
mIsMain = isMain;
mFileVersion = fileVersion;
mFileSize = fileSize;
}
}
private static final XAPKFile[] xAPKS = {
new XAPKFile(
true, // true signifies a main file
1000006, // the version of the APK that the file was uploaded against
443975466L // the length of the file in bytes
)
};
static private final float SMOOTHING_FACTOR = 0.005f;
@Override
public void onStart() {
Log.d(TAG, "ON START");
if (null != mDownloaderClientStub) {
mDownloaderClientStub.connect(context);
}
super.onStart();
}
@Override
public void onStop() {
Log.d(TAG, "ON STOP");
if (null != mDownloaderClientStub) {
mDownloaderClientStub.disconnect(context);
}
super.onStop();
}
@Override
public void onServiceConnected(Messenger m) {
Log.d(TAG, "ON SERVICE CONNECTED");
mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
}
@Override
public void onDownloadStateChanged(int newState) {
Log.d(TAG, "ON DOWNLOAD STATE CHANGED: " + newState);
setState(newState);
boolean showDashboard = true;
boolean showCellMessage = false;
boolean paused;
boolean indeterminate;
switch (newState) {
case IDownloaderClient.STATE_IDLE:
// STATE_IDLE means the service is listening, so it's
// safe to start making calls via mRemoteService.
paused = false;
indeterminate = true;
break;
case IDownloaderClient.STATE_CONNECTING:
case IDownloaderClient.STATE_FETCHING_URL:
showDashboard = true;
paused = false;
indeterminate = true;
break;
case IDownloaderClient.STATE_DOWNLOADING:
paused = false;
showDashboard = true;
indeterminate = false;
break;
case IDownloaderClient.STATE_FAILED_CANCELED:
case IDownloaderClient.STATE_FAILED:
case IDownloaderClient.STATE_FAILED_FETCHING_URL:
case IDownloaderClient.STATE_FAILED_UNLICENSED:
paused = true;
showDashboard = false;
indeterminate = false;
break;
case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
showDashboard = false;
paused = true;
indeterminate = false;
showCellMessage = true;
break;
case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
paused = true;
indeterminate = false;
break;
case IDownloaderClient.STATE_PAUSED_ROAMING:
case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
paused = true;
indeterminate = false;
break;
case IDownloaderClient.STATE_COMPLETED:
showDashboard = false;
paused = false;
indeterminate = false;
validateXAPKZipFiles();
return;
default:
paused = true;
indeterminate = true;
showDashboard = true;
}
}
@Override
public void onDownloadProgress(DownloadProgressInfo progress) {
Log.d(TAG, "ON DOWNLOAD PROGESS: " + progress);
}
boolean expansionFilesDelivered() {
Log.d(TAG, "IF EXPANSION FILE IS DELIVERED");
for (XAPKFile xf : xAPKS) {
String fileName = Helpers.getExpansionAPKFileName(context, xf.mIsMain, xf.mFileVersion);
if (!Helpers.doesFileExist(context, fileName, xf.mFileSize, false)) {
Log.d(TAG, "EXPANSION FILE DOESN'T EXIST");
return false;
}
}
Log.d(TAG, "EXPANSION FILE EXIST");
return true;
}
}
My problem is that onServiceConnected
is never called although I call mDownloaderClientStub.connect(context);
Does anybody know why it is not called? I'm getting no error either.
答案 0 :(得分:0)
无法建立服务连接,因为您将不正确的Class
对象传递到扩展库方法中。
您应该将调用DownloaderClientMarshaller
中的参数从DownloaderService.class
更改为MyDownloaderService.class
,或者使用哪个类来扩展基础DownloaderService
。另外,请确保您的应用清单中定义了您的服务。
// use the correct service class!
int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(context, pendingIntent, MyDownloaderService.class);
// when creating a stub also
mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, MyDownloaderService.class);
我建议使用Better APK Expansion包中包含的更新的下载程序库。它解决了这个问题和其他问题,还提供了简化的API,最大限度地减少了自己射击脚的机会。