Tracking changes on /proc/uid_stat/myAppUID - android

Does someone know is there a way I can get notified on file change in /proc/uid_stat/myAppUID folder?
I want to track data usage of my app. The file is there and when I read it manually using BufferedReader, I get the data traffic.
I tried using FileObserver class and also RecursiveFileObserver but I don't get any callback when data usage change. My guess is that it doesn't work on virtual file system. I would like to get notified from linux when these files changes, because looping constantly through files is not a valid option for me.
Here is the code I used:
path = "/proc/uid_stat/"+getApplicationInfo().uid;
observer = new FileObserver(path) {
#Override
public void onEvent(int event, String file) {
Toast.makeText(getApplicationContext(), file + " was changed!", Toast.LENGTH_LONG).show();
}
}
};
observer.startWatching();

Related

How to resume a file download in firebase Storage?

I need to download a big file using firebase storage,
it is possible my user face network interruption through the download and i would like to be able to resume this download task.
FileDownloadTask downloadTask = firebaseStorage.getReferenceFromUrl(url).getFile(localFile);
downloadTask.addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.v(TAG, "progress" + taskSnapshot.getBytesTransferred());;
}
});
downloadTask.addOnFailureListener(e -> {
CustomLogger.v(TAG, "downloadTask, on Failed: " + courseId);
deleteFilesAfterError(courseId);
});
downloadTask.addOnPausedListener(taskSnapshot
-> CustomLogger.v(TAG, "downloadTask, on paused: " + courseId));
the two last listener are never called. when i set up the complete listener:
downloadTask.addOnCompleteListener()
my app crashes.
even when i set up :
firebaseStorage.setMaxDownloadRetryTimeMillis(2000);
then switch off my network in the middle of a download, the downloadTask fail listener is not trigger. The task still seems to be "in progress" but there no data are downloaded. The documentation is poor on this feature. how am i supposed to implement it?
the fail listener seems broken
when i add a completeListener it crashes my app
firebaseStorage.setMaxDownloadRetryTimeMillis does not seems to have
any effect
any idea on how to tackle a resume feature for a file download?
When you say you switch off your network in the middle of the download, how are you doing this?
The reason I ask is because if you are leaving the activity temporarily to turn off the network stream then your problem might be an activity lifecycle problem.
If you leave the activity and do not handle this in the onPause(), onStop() and so on then you might experience this sort of behavior.
Other than that, your code looks similar to the docs (and my code that works fine), other than the fact that you didn't 'chain' your methods to add the listeners (shouldn't be a problem).
However, your code that you copied and pasted here is partially folded and does not show whether or not you are overiding the onFailure(Exception e) method, but I assume it is folded up in that arrow.
If you are still working on this, can you post the trace for any errors you're receiving on the crash?
Here is a block of code that works during network failure, just for a reference:
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReferenceFromUrl("gs://your-bucket/");
storageReference.getFile(localFile).addOnSuccessListener(YourActivity.this, new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
}
}
}).addOnFailureListener(YourActivity.this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
}

Instabug, is it possible to deactivate the shake for feedback, if there is no internet access?

I have a networkStateReceiver, that checks if I have internet or not.
If I do, I reinitiate instabug, if not, I want to deactivate. How can I do that?
I tried just setting it as null, but it doesn't work.
if(haveConnectedMobile || haveConnectedWifi){
//TODO will need to make a queue, and go through all that queue
PSLocationCenter.getInstance().initInstabug();
}else{
PSLocationCenter.getInstance().instabug = null;
}
This is my init:
public void initInstabug() {
String[] feedbackArray = getResources().getStringArray(R.array.feedback);
String randomStr = feedbackArray[new Random().nextInt(feedbackArray.length)];
Instabug.DEBUG = true;
instabug = Instabug.initialize(this)
.setAnnotationActivityClass(InstabugAnnotationActivity.class)
.setShowIntroDialog(true, PSTimelineActivity.class)
.enableEmailField(true, false)
.setEnableOverflowMenuItem(true)
.setDebugEnabled(true)
.setCommentRequired(true)
.setPostFeedbackMessage(randomStr)
.setPostBugReportMessage(randomStr) //TODO will be the post report message, random from array
.setCommentFieldHint("Please describe what went wrong")
.setPreSendingRunnable(new Runnable() {
#Override
public void run() {
String[] files = new String[2];
files[0] = Environment.getExternalStorageDirectory() + "/Passenger/passenger_log.txt";
files[1] = Environment.getExternalStorageDirectory() + "/Passenger/passenger_log2.txt";
Compress compress = new Compress(files, Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
compress.zip(new CrudStateCallback() {
#Override
public void onResponse(String string) {
Log.i("", "ended making the archive");
}
});
}
})
.attachFileAtLocation(Environment.getExternalStorageDirectory() + "/Passenger/log.zip");
}
You can use this code to disable Instabug automatic invocation:
Instabug.getInstance().setInvocationEvent(IBGInvocationEvent.IBGInvocationEventNone)
This way it won't be invoked automatically. This will only affect the next Activity though (not the current one). You may force to stop and restart all listeners by calling onPause and onResume on the current Activity. (We may address that soon though, so that such changes are applied on the currently running Activity).
Don't forget to also enable the shake invocation event when internet access is restored.
Please keep in mind that Instabug SDK already caches all reports and will re-attempt to send them on next app launch until they're uploaded successfully.
Just wanted to post the updated answer.
The newer SDK has changed the name and now you can disable it by the following code:
Instabug.changeInvocationEvent(InstabugInvocationEvent.NONE)
Notice, if you want to disable it for entire application, just call this method in your Application class

Android - mount OBB in service

I'm writing an application which includes a service and mounts an OBB file from service.
I'm referring this documentation: http://developer.android.com/reference/android/os/storage/StorageManager.html
http://developer.android.com/reference/android/os/storage/OnObbStateChangeListener.html
However, onObbStateChange() callback never happens.
If I use the very same code in application's activity, it successfully mounts and I get access to the internals of the OBB file.
Below is the code snippet:
// Extend OnObbStateChangeListener class
public class MyService extends Service {
static StorageManager storageManager;
static class Observer extends OnObbStateChangeListener {
#Override
public void onObbStateChange(String path, int state) {
Log.v(TAG, path + " " + state); // 1
}
}
}
// Mount obb
public void getMountedPath() {
File file = new File("<path to obb file>");
if(!obbFile.exists())
return;
Observer observer = new Observer();
boolean state = storageManager.mountObb(obbFile.getAbsolutePath(), null, obbObserver);
Log.v(TAG, "state: " + state); // 2 It always prints TRUE
}
Any idea why it's not able to mount from my service class?
More observations:
When I execute the app for the first time, I get mount state as TRUE in the second log statement and onObbStateChange() doesn't get invoked.
However, when I run my app second time, I see the first log from onObbStateChange() and it prints state as 24 which means ERROR_ALREADY_MOUNTED.
So it seems that the obb got mounted in the first attempt, however, onObbStateChange() didn't get invoked for some reason.
Any explanations on this behavior?

Android google drive file change listener not working

I have registered the change listener to google drive file as follows-
DriveFile driveFile = Drive.DriveApi.getFile(mGoogleApiClient, mSelectedFileId);
driveFile.addChangeListener(mGoogleApiClient,changeListener);
final private Listener<ChangeEvent> changeListener = new Listener<ChangeEvent>() {
#Override
public void onEvent(ChangeEvent event) {
Toast.makeText(MainActivity.this, "File Changed", Toast.LENGTH_LONG).show();
}
};
when i changing file content through web browser,then change listener is called.What is correct way to listen drive file changes?
I assume you mean change listener isn't called.
Changes made on the web won't immediately be reflected on the device. It will happen the next time that the device finds out about updates. You can speed that up by interacting with the API, or calling requestSync.

Dropbox Sync Api. Not seeing the File on Dropbox the first time round

I have a DropboxHelper Class that is handling downloading and uploading from dropbox.
Downloading works fine but when I try to upload from dropbox the first time the code is called. The following Line is false
if (dropboxFileSystem.isFile(dropboxPath)) {
}
It returns false. Tell the app to try again and this time it sees the file and uploads it to the app. Below is some of the code I am using for the class. Debug seems to incdicate the dropbox api has not completing started / synced the first time
public class DropBoxHelper {
public DropBoxHelper(Context pContext) {
context = pContext;
defineVariables();
}
private void defineVariables() {
dropboxAccountManager = DbxAccountManager.getInstance(context.getApplicationContext(), DROPBOX_APP_KEY, DROPBOX_APP_SECRET);
dropboxPath = new DbxPath(DbxPath.ROOT, DROPBOX_FILE_NAME);
}
public boolean importFromDropbox() {
try {
dropboxFileSystem = DbxFileSystem.forAccount(dropboxAccountManager.getLinkedAccount());
if (dropboxFileSystem.isFile(dropboxPath)) {
DbxFile databaseFileonDropbox = dropboxFileSystem.open(dropboxPath);
try {
// Do Copy
} finally {
Log.i(DEBUG_TAG, "Closing File");
databaseFileonDropbox.close();
}
}
Any ideas on why the copy fails first time.
Thanks
I'm not 100% sure, but I believe you need to use dropboxFileSystem.awaitFirstSync() to make sure at least one sync with the server has happened before you try to find the file.
An alternative might be to just call dropboxFileSystem.open(...) directly and handle the exception that's raised if the file doesn't exist.

Categories

Resources