|
| 1 | +package protect.card_locker.importexport; |
| 2 | + |
| 3 | +import android.app.Notification; |
| 4 | +import android.app.Service; |
| 5 | +import android.content.Intent; |
| 6 | +import android.database.sqlite.SQLiteDatabase; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Build; |
| 9 | +import android.os.IBinder; |
| 10 | +import android.util.Log; |
| 11 | +import android.widget.Toast; |
| 12 | + |
| 13 | +import androidx.annotation.Nullable; |
| 14 | + |
| 15 | +import java.io.FileNotFoundException; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.OutputStream; |
| 18 | +import java.io.OutputStreamWriter; |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | + |
| 21 | +import protect.card_locker.DBHelper; |
| 22 | +import protect.card_locker.ImportExportActivity; |
| 23 | +import protect.card_locker.ImportExportTask; |
| 24 | +import protect.card_locker.NotificationHelper; |
| 25 | +import protect.card_locker.R; |
| 26 | +import protect.card_locker.async.TaskHandler; |
| 27 | + |
| 28 | +public class ImportExportService extends Service { |
| 29 | + private final String TAG = "Catima"; |
| 30 | + |
| 31 | + public static final String ACTION = "action"; |
| 32 | + public static final String FORMAT = "format"; |
| 33 | + public static final String PASSWORD = "password"; |
| 34 | + |
| 35 | + public static final String ACTION_IMPORT = "import"; |
| 36 | + public static final String ACTION_EXPORT = "export"; |
| 37 | + |
| 38 | + @Nullable |
| 39 | + @Override |
| 40 | + public IBinder onBind(Intent intent) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public int onStartCommand(Intent intent, int flags, int startId) { |
| 46 | + Log.e("CATIMA", "Started service"); |
| 47 | + |
| 48 | + Uri uri = intent.getData(); |
| 49 | + String action = intent.getStringExtra(ACTION); |
| 50 | + String format = intent.getStringExtra(FORMAT); |
| 51 | + String password = intent.getStringExtra(PASSWORD); |
| 52 | + |
| 53 | + if (action.equals(ACTION_IMPORT)) { |
| 54 | + Log.e("CATIMA", "Import requested"); |
| 55 | + Notification.Builder notificationBuilder = new NotificationHelper().createNotification(this, NotificationHelper.CHANNEL_IMPORT, getString(R.string.importing), getString(R.string.importing)); |
| 56 | + |
| 57 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 58 | + notificationBuilder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE); |
| 59 | + } |
| 60 | + |
| 61 | + startForeground(NotificationHelper.IMPORT_ID, notificationBuilder.build()); |
| 62 | + } else { |
| 63 | + Log.e("CATIMA", "Export requested"); |
| 64 | + Notification.Builder notificationBuilder = new NotificationHelper().createNotification(this, NotificationHelper.CHANNEL_EXPORT, getString(R.string.exporting), getString(R.string.exporting)); |
| 65 | + |
| 66 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { |
| 67 | + notificationBuilder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE); |
| 68 | + } |
| 69 | + |
| 70 | + startForeground(NotificationHelper.EXPORT_ID, notificationBuilder.build()); |
| 71 | + |
| 72 | + ImportExportResult result; |
| 73 | + |
| 74 | + OutputStream stream; |
| 75 | + try { |
| 76 | + stream = getContentResolver().openOutputStream(uri); |
| 77 | + } catch (FileNotFoundException e) { |
| 78 | + throw new RuntimeException(e); |
| 79 | + } |
| 80 | + |
| 81 | + final SQLiteDatabase database = new DBHelper(this).getWritableDatabase(); |
| 82 | + |
| 83 | + try { |
| 84 | + OutputStreamWriter writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8); |
| 85 | + result = MultiFormatExporter.exportData(this, database, stream, DataFormat.valueOf(format), password.toCharArray()); |
| 86 | + writer.close(); |
| 87 | + } catch (IOException e) { |
| 88 | + result = new ImportExportResult(ImportExportResultType.GenericFailure, e.toString()); |
| 89 | + Log.e(TAG, "Unable to export file", e); |
| 90 | + } |
| 91 | + |
| 92 | + Log.i(TAG, "Export result: " + result); |
| 93 | + } |
| 94 | + |
| 95 | + return START_REDELIVER_INTENT; |
| 96 | + } |
| 97 | +} |
0 commit comments