How to detect when app was restored using Backup Service? - android

In my app user can choose custom files to be used instead of some default settings. File's info is stored by its URI and I have hard time handling case of Backup Service restoring whole app, when all the restored URIs will be invalid.
App will turn to using default settings whenever it detects invalid URIs (not pointing to custom files), however I would want to inform the user about this fact after restore is performed, so he knows why app is using default settings.
Is there any way to detect when app was restored using BackupAgent?

So far I didn't find any proper tool for detecting when app was restored with use of a Backup Service, however I found a workaround:
context.packageManager.getPackageInfo(myPackageName, 0).firstInstallTime always returns time of install of the current instance of the app, so it is possible to store this time using backed-up SharedPreferences and then comparing it with the time received from packageManager - if it is "newer" than the one stored in SharedPreferences, then it means the app has been restored by a Backup Service.

Related

In Android 11, how is it possible, for an app to retain data after uninstallation, without SAF user interaction to pick a file/ folder location?

Based on Is it possible to persisit a file after app uninstall with Android 11?
In Android 11, It seems like almost impossible to retain user data locally, after app uninstallation. Unless we are using SAF, which requires explicit user interaction, to select file/ dialog using this system UI
Or, by using android:hasFragileUserData="true" flag, which will pop up the following UI during uninstallation - https://www.xda-developers.com/android-10-manifest-flag-developers-retain-app-data-before-uninstalling/
However, to my suprise, I do notice a popular app in the market, which able to cleanly retain user multiple backup data, with 0 user interaction, and not using android:hasFragileUserData="true" flag method.
This is a video, to show how it is able to cleanly read the retained data, after re-installation (I have already did a clean uninstall, before capturing the video) - https://www.youtube.com/watch?v=UqG4n_0xmVI
Does anyone has any idea, how I can achieve such "magic"?

Perform action after Clear Data of my application

I want to be notified when the user or any other application Clear the data of my application (e.g. User click on Clear data button from app info). Is there is any way to know that this action was taken.
I expect to find Broadcast intent that I can receive to handle my action! is it possible? IF not are there any other way to know or not?
I expect to find Broadcast intent that I can receive to handle my action!
There is one, but your app cannot receive it. Presumably, this is to prevent malware from trying to interfere with this process somehow. If the user wants your data to be cleared, please allow it.
IF not are there any other way to know or not?
IMHO, an app should be idempotent with respect to application data. In other words, the app should treat a fresh installation identically as it would treat starting up for the first time after its application data has been cleared, which would also be identical to the app having been uninstalled and later reinstalled. Any other behavior either has privacy ramifications (e.g., trying to use some device-specific identifier and record whether the app had been installed here before) or clutter ramifications (e.g., storing some file on common external storage and checking for its existence when your app realizes that it has no application data on internal or its portion of external storage).

How to protect/detect database restore on Android device?

I have an app for Android, that saves data to sqlite database in a common way. As the user works with the application, the data is changed etc. So far no problem...
But when the user use some back-up software (like Titanium Backup or others), make a backup of the application, he can restore the data to old state. I need the way to protect application from this or to detect the restoration and handle it.
The simple workflow:
Install APP
Work with APP
Reach the STATE1 of APP's database
Back-up the APP (with any backup/restore application, the device can be rooted)
Work with APP
Reach the STATE2 of APP's database
Restores the APP (or just data) to STATE1 - this is the point I need to deny or detect on the next execution of the APP.
So far I played with the Access-time detection and comparsion, but it seems to be really un-reliable through different devices and ROMs.
Thank you.
To do this you need to save some state off the device, or at least outside of the data directory. The easiest way is probably to save the fingerprint of the DB file in some 'hidden' directory on external storage (SD card). Or if your app has Web login, etc. store the fingerprint for each user.
In any case, the user has full control over the device so you can't prevent this 100%. Your app needs to gracefully handle any changes in the DB or simply fail if it detects a fatal inconsistency.

Why does updating an app clear its defaults?

Let's say you have an app that declares itself able to handle a system intent like Phone and that the user selects it as the default app. When the market updates the app, why does it clear the defaults? Is there a way to prevent that, so if I wanted Dialer 1.0 to handle the phone button, Dialer 2.0 will still do it without me having to re-select it.
The defaults map to specific implementations in the application (the name of the class implementing an activity), which can change when an application is updated. To be conservative and safe, the platform has traditionally cleared those when an application is updated because they may change and result in a reference to an activity that no longer exists. There is nothing you can do in your app to prevent this from happening.

Is there anyway to know that application launches firsttime?

Is there anyway to know that when an application is launched for the firsttime? I dont want to use sharedpref because when the user cleardata manually it clears all the data.
That is the point of clearing data, the user may want to tell your app to consider itself freshly installed without having to necessarily uninstall the app. You should not be trying to break that expectation. That said.....
You could create a file on the SD card and check for the existence of that file to determine if your app is being run for the first time.
Do not use the normal openFileOutput() calls in android as that will be cleared when the user clears data as well.
Unless the user wipes all files on the SD card, the file should remain in existence. Also, you can do both, a shared preference and the file and then check for one or the other, just in case.
Use your own server. Just store the unique android id on your server in the first time user launches your application. But you need internet for this.

Categories

Resources