mirror of
https://github.com/nextcloud/android.git
synced 2024-11-24 22:25:44 +03:00
added log output
fix multiple calls of ConnectivityActionReceiver
This commit is contained in:
parent
d3be204240
commit
8e9cf480ab
5 changed files with 86 additions and 44 deletions
|
@ -6,65 +6,90 @@ import android.content.Context;
|
|||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.NetworkInfo.State;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import com.owncloud.android.files.InstantUploadBroadcastReceiver;
|
||||
import com.owncloud.android.lib.common.utils.Log_OC;
|
||||
|
||||
/**
|
||||
* Receives all connectivity action from Android OS at all times and performs required OC actions.
|
||||
* For now that are:
|
||||
* - Signal connectivity to {@link FileUploadService}.
|
||||
*
|
||||
* Later can be added:
|
||||
* - Signal connectivity to download service, deletion service, ...
|
||||
* - Handle offline mode (cf. https://github.com/owncloud/android/issues/162)
|
||||
*
|
||||
* Receives all connectivity action from Android OS at all times and performs
|
||||
* required OC actions. For now that are: - Signal connectivity to
|
||||
* {@link FileUploadService}.
|
||||
*
|
||||
* Later can be added: - Signal connectivity to download service, deletion
|
||||
* service, ... - Handle offline mode (cf.
|
||||
* https://github.com/owncloud/android/issues/162)
|
||||
*
|
||||
* @author LukeOwncloud
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class ConnectivityActionReceiver extends BroadcastReceiver {
|
||||
private static final String TAG = "ConnectivityActionReceiver";
|
||||
|
||||
@Override
|
||||
public void onReceive(final Context context, Intent intent) {
|
||||
// if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||
Log.v(TAG, "action: " + intent.getAction());
|
||||
Log.v(TAG, "component: " + intent.getComponent());
|
||||
Bundle extras = intent.getExtras();
|
||||
if (extras != null) {
|
||||
for (String key : extras.keySet()) {
|
||||
Log.v(TAG, "key [" + key + "]: " + extras.get(key));
|
||||
}
|
||||
} else {
|
||||
Log.v(TAG, "no extras");
|
||||
// LOG ALL EVENTS:
|
||||
Log.v(TAG, "action: " + intent.getAction());
|
||||
Log.v(TAG, "component: " + intent.getComponent());
|
||||
Bundle extras = intent.getExtras();
|
||||
if (extras != null) {
|
||||
for (String key : extras.keySet()) {
|
||||
Log.v(TAG, "key [" + key + "]: " + extras.get(key));
|
||||
}
|
||||
} else {
|
||||
Log.v(TAG, "no extras");
|
||||
}
|
||||
|
||||
if (InstantUploadBroadcastReceiver.isOnline(context)) {
|
||||
FileUploadService.retry(context);
|
||||
|
||||
/**
|
||||
* Just checking for State.CONNECTED will is not good enough, as it ends here multiple times.
|
||||
* Work around from:
|
||||
* http://stackoverflow.com/questions/17287178/connectivitymanager-getactivenetworkinfo-returning-true-when-internet-is-off
|
||||
*/
|
||||
if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
||||
NetworkInfo networkInfo =
|
||||
intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||
if(networkInfo.isConnected()) {
|
||||
Log.d(TAG, "Wifi is connected: " + String.valueOf(networkInfo));
|
||||
wifiConnected(context);
|
||||
}
|
||||
// }
|
||||
} else if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||
ConnectivityManager cm =
|
||||
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
|
||||
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
|
||||
! networkInfo.isConnected()) {
|
||||
Log.d(TAG, "Wifi is disconnected: " + String.valueOf(networkInfo));
|
||||
wifiDisconnected(context);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static public void enable(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName compName =
|
||||
new ComponentName(context.getApplicationContext(),
|
||||
ConnectivityActionReceiver.class);
|
||||
pm.setComponentEnabledSetting(
|
||||
compName,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
|
||||
private void wifiConnected(Context context) {
|
||||
Log_OC.d(TAG, "FileUploadService.retry() called by onReceive()");
|
||||
FileUploadService.retry(context);
|
||||
}
|
||||
|
||||
static public void disable(Context context) {
|
||||
|
||||
private void wifiDisconnected(Context context) {
|
||||
|
||||
}
|
||||
|
||||
static public void enableActionReceiver(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName compName =
|
||||
new ComponentName(context.getApplicationContext(),
|
||||
ConnectivityActionReceiver.class);
|
||||
pm.setComponentEnabledSetting(
|
||||
compName,
|
||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
ComponentName compName = new ComponentName(context.getApplicationContext(), ConnectivityActionReceiver.class);
|
||||
pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
}
|
||||
|
||||
static public void disableActionReceiver(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ComponentName compName = new ComponentName(context.getApplicationContext(), ConnectivityActionReceiver.class);
|
||||
pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
PackageManager.DONT_KILL_APP);
|
||||
}
|
||||
}
|
|
@ -254,6 +254,7 @@ public class FileUploadService extends IntentService {
|
|||
}
|
||||
|
||||
if(InstantUploadBroadcastReceiver.isOnline(getApplicationContext())) {
|
||||
Log_OC.d(TAG, "FileUploadService.retry() called by onCreate()");
|
||||
FileUploadService.retry(getApplicationContext());
|
||||
}
|
||||
}
|
||||
|
@ -281,7 +282,10 @@ public class FileUploadService extends IntentService {
|
|||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
Log_OC.i(TAG, "onHandleIntent start");
|
||||
Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size() + " - before adding new uploads.");
|
||||
if (intent == null || intent.hasExtra(KEY_RETRY)) {
|
||||
Log_OC.d(TAG, "Receive null intent.");
|
||||
// service was restarted by OS (after return START_STICKY and kill
|
||||
// service) or connectivity change was detected. ==> check persistent upload
|
||||
// list.
|
||||
|
@ -301,7 +305,7 @@ public class FileUploadService extends IntentService {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
Log_OC.d(TAG, "Receive upload intent.");
|
||||
UploadSingleMulti uploadType = (UploadSingleMulti) intent.getSerializableExtra(KEY_UPLOAD_TYPE);
|
||||
if (uploadType == null) {
|
||||
Log_OC.e(TAG, "Incorrect or no upload type provided");
|
||||
|
@ -406,17 +410,21 @@ public class FileUploadService extends IntentService {
|
|||
|
||||
// at this point mPendingUploads is filled.
|
||||
|
||||
Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size());
|
||||
Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size() + " - before uploading.");
|
||||
|
||||
try {
|
||||
Iterator<String> it = mPendingUploads.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
UploadDbObject uploadDbObject = mPendingUploads.get(it.next());
|
||||
String up = it.next();
|
||||
Log_OC.d(TAG, "Calling uploadFile for " + up);
|
||||
UploadDbObject uploadDbObject = mPendingUploads.get(up);
|
||||
boolean uploadSuccessful = uploadFile(uploadDbObject);
|
||||
}
|
||||
} catch (ConcurrentModificationException e) {
|
||||
// for now: ignore. TODO: fix this.
|
||||
}
|
||||
Log_OC.i(TAG, "mPendingUploads size:" + mPendingUploads.size() + " - after uploading.");
|
||||
Log_OC.i(TAG, "onHandleIntent end");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -951,6 +959,7 @@ public class FileUploadService extends IntentService {
|
|||
}
|
||||
|
||||
public static void retry(Context context) {
|
||||
Log_OC.d(TAG, "FileUploadService.retry()");
|
||||
Intent i = new Intent(context, FileUploadService.class);
|
||||
i.putExtra(FileUploadService.KEY_RETRY, true);
|
||||
context.startService(i);
|
||||
|
|
|
@ -459,6 +459,10 @@ public class UploadFileOperation extends RemoteOperation {
|
|||
}
|
||||
|
||||
public void cancel() {
|
||||
if (mUploadOperation == null) {
|
||||
Log_OC.e(TAG, "UploadFileOperation.cancel(): mUploadOperation == null for file: " + mFile + ". Fix that.");
|
||||
return;
|
||||
}
|
||||
mUploadOperation.cancel();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1221,6 +1221,8 @@ OnSslUntrustedCertListener, OnEnforceableRefreshListener {
|
|||
if (sameAccount && sameFile && detailFragmentIsShown) {
|
||||
if (uploadWasFine) {
|
||||
setFile(getStorageManager().getFileByPath(uploadedRemotePath));
|
||||
} else {
|
||||
//TODO remove upload progress bar after upload failed.
|
||||
}
|
||||
if (renamedInUpload) {
|
||||
String newName = (new File(uploadedRemotePath)).getName();
|
||||
|
|
|
@ -70,6 +70,7 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
|
|||
// no break; to start upload immediately.
|
||||
case UPLOAD_LATER:
|
||||
case UPLOAD_FAILED_RETRY:
|
||||
Log_OC.d(TAG, "FileUploadService.retry() called by onUploadItemClick()");
|
||||
FileUploadService.retry(this);
|
||||
break;
|
||||
default:
|
||||
|
@ -89,6 +90,7 @@ public class UploadListActivity extends FileActivity implements UploadListFragme
|
|||
boolean retval = true;
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_retry_uploads: {
|
||||
Log_OC.d(TAG, "FileUploadService.retry() called by onMenuItemSelected()");
|
||||
FileUploadService.retry(this);
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue