Make app direct boot aware

This commit is contained in:
xynngh 2020-07-02 19:12:04 +04:00
parent 3dc63ee45b
commit 8179fd5e60
5 changed files with 87 additions and 9 deletions

View File

@ -62,6 +62,7 @@
<activity
android:name=".DummyDialerActivity"
android:autoRemoveFromRecents="true"
android:directBootAware="true"
android:excludeFromRecents="true"
android:noHistory="true"
android:theme="@style/DialogBackgroundTheme">
@ -82,6 +83,7 @@
<receiver
android:name=".CallReceiver"
android:directBootAware="true"
android:enabled="true"
android:exported="true">
<intent-filter>
@ -93,6 +95,7 @@
<service
android:name=".CallScreeningServiceImpl"
android:directBootAware="true"
android:permission="android.permission.BIND_SCREENING_SERVICE">
<intent-filter>
<action android:name="android.telecom.CallScreeningService" />

View File

@ -2,6 +2,8 @@ package dummydomain.yetanothercallblocker;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import org.conscrypt.Conscrypt;
import org.greenrobot.eventbus.EventBus;
@ -17,6 +19,14 @@ public class App extends Application {
@SuppressLint("StaticFieldLeak")
private static Settings settings;
public static App getInstance() {
return instance;
}
public static Settings getSettings() {
return settings;
}
@Override
public void onCreate() {
super.onCreate();
@ -25,7 +35,9 @@ public class App extends Application {
Security.insertProviderAt(Conscrypt.newProvider(), 1);
settings = new Settings(this);
new DeviceProtectedStorageMigrator().migrate(this);
settings = new Settings(getDeviceProtectedStorageContext());
settings.init();
EventBus.builder()
@ -38,15 +50,15 @@ public class App extends Application {
NotificationHelper.createNotificationChannels(this);
Config.init(this, settings);
Config.init(getDeviceProtectedStorageContext(), settings);
}
public static App getInstance() {
return instance;
private Context getDeviceProtectedStorageContext() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return createDeviceProtectedStorageContext();
} else {
return this;
}
public static Settings getSettings() {
return settings;
}
}

View File

@ -0,0 +1,61 @@
package dummydomain.yetanothercallblocker;
import android.content.Context;
import android.os.Build;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import dummydomain.yetanothercallblocker.data.SiaConstants;
import dummydomain.yetanothercallblocker.sia.utils.FileUtils;
public class DeviceProtectedStorageMigrator {
public void migrate(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) return;
try {
Context dpsContext = context.createDeviceProtectedStorageContext();
if (new Settings(dpsContext).getInt(Settings.SYS_PREFERENCES_VERSION, -1) != -1) return;
if (new Settings(context).getInt(Settings.SYS_PREFERENCES_VERSION, -1) == -1) return;
copyDir(context, dpsContext, SiaConstants.SIA_PATH_PREFIX);
copyDir(context, dpsContext, SiaConstants.SIA_SECONDARY_PATH_PREFIX);
dpsContext.moveSharedPreferencesFrom(context, SiaConstants.SIA_PROPERTIES);
dpsContext.moveSharedPreferencesFrom(context, context.getPackageName() + "_preferences");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void copyDir(Context srcContext, Context dstContext, String dir) throws IOException {
File srcDir = new File(srcContext.getFilesDir(), dir);
if (srcDir.exists()) {
File dstDir = new File(dstContext.getFilesDir(), dir);
dstDir.mkdir();
copyDirectoryContent(srcDir, dstDir);
FileUtils.delete(srcDir);
}
}
private static void copyDirectoryContent(File src, File dst) throws IOException {
for (String file : src.list()) {
File srcFile = new File(src, file);
File dstFile = new File(dst, file);
dstFile.createNewFile();
try (FileChannel source = new FileInputStream(srcFile).getChannel();
FileChannel destination = new FileOutputStream(dstFile).getChannel()) {
destination.transferFrom(source, 0, source.size());
}
}
}
}

View File

@ -20,7 +20,7 @@ public class Settings extends GenericSettings {
public static final String PREF_COUNTRY_CODE_OVERRIDE = "countryCodeOverride";
public static final String PREF_COUNTRY_CODE_FOR_REVIEWS_OVERRIDE = "countryCodeForReviewsOverride";
private static final String SYS_PREFERENCES_VERSION = "__preferencesVersion";
static final String SYS_PREFERENCES_VERSION = "__preferencesVersion";
private static final int PREFERENCES_VERSION = 1;

View File

@ -99,6 +99,8 @@ public class SettingsActivity extends AppCompatActivity
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
getPreferenceManager().setStorageDeviceProtected();
setPreferencesFromResource(R.xml.root_preferences, rootKey);
initRootScreen(rootKey);