Skip to content

Commit fa5b37a

Browse files
WIP
1 parent 6ed5e52 commit fa5b37a

File tree

6 files changed

+150
-10
lines changed

6 files changed

+150
-10
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<uses-sdk tools:overrideLibrary="com.google.zxing.client.android" />
1313

1414
<uses-permission android:name="android.permission.CAMERA" />
15+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
16+
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
1517
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="23" />
1618

1719
<uses-feature
@@ -186,5 +188,6 @@
186188
<action android:name="android.service.controls.ControlsProviderService" />
187189
</intent-filter>
188190
</service>
191+
<service android:name=".importexport.ImportExportService"/>
189192
</application>
190193
</manifest>

app/src/main/java/protect/card_locker/ImportExportActivity.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import protect.card_locker.importexport.DataFormat;
3636
import protect.card_locker.importexport.ImportExportResult;
3737
import protect.card_locker.importexport.ImportExportResultType;
38+
import protect.card_locker.importexport.ImportExportService;
3839

3940
public class ImportExportActivity extends CatimaAppCompatActivity {
4041
private ImportExportActivityBinding binding;
@@ -80,15 +81,13 @@ protected void onCreate(Bundle savedInstanceState) {
8081
Log.e(TAG, "Activity returned NULL uri");
8182
return;
8283
}
83-
try {
84-
OutputStream writer = getContentResolver().openOutputStream(uri);
85-
Log.e(TAG, "Starting file export with: " + result.toString());
86-
startExport(writer, uri, exportPassword.toCharArray(), true);
87-
} catch (IOException e) {
88-
Log.e(TAG, "Failed to export file: " + result.toString(), e);
89-
onExportComplete(new ImportExportResult(ImportExportResultType.GenericFailure, result.toString()), uri);
90-
}
9184

85+
Intent serviceIntent = new Intent(this, ImportExportService.class);
86+
serviceIntent.setData(uri);
87+
serviceIntent.putExtra(ImportExportService.ACTION, ImportExportService.ACTION_EXPORT);
88+
serviceIntent.putExtra(ImportExportService.FORMAT, DataFormat.Catima.name());
89+
serviceIntent.putExtra(ImportExportService.PASSWORD, exportPassword);
90+
startService(serviceIntent);
9291
});
9392
fileOpenLauncher = registerForActivityResult(new ActivityResultContracts.GetContent(), result -> {
9493
if (result == null) {

app/src/main/java/protect/card_locker/ImportExportTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public ImportExportResult call() {
136136
return doInBackground();
137137
}
138138

139-
interface TaskCompleteListener {
139+
public interface TaskCompleteListener {
140140
void onTaskComplete(ImportExportResult result, DataFormat format);
141141
}
142142

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package protect.card_locker;
2+
3+
import static android.content.Context.NOTIFICATION_SERVICE;
4+
5+
import android.app.Notification;
6+
import android.app.NotificationChannel;
7+
import android.app.NotificationManager;
8+
import android.content.Context;
9+
10+
public class NotificationHelper {
11+
12+
// Do not change these IDs!
13+
public static final String CHANNEL_IMPORT = "import";
14+
public static final String CHANNEL_EXPORT = "export";
15+
16+
public static final int IMPORT_ID = 100;
17+
public static final int EXPORT_ID = 101;
18+
19+
public Notification.Builder createNotification(Context context, String channel, String title, String message) {
20+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
21+
NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
22+
NotificationChannel notificationChannel = new NotificationChannel(channel, getChannelName(channel), NotificationManager.IMPORTANCE_DEFAULT);
23+
notificationManager.createNotificationChannel(notificationChannel);
24+
}
25+
26+
return new Notification.Builder(context)
27+
.setContentTitle(title)
28+
.setContentText(message);
29+
}
30+
31+
private String getChannelName(String channel) {
32+
switch(channel) {
33+
case CHANNEL_IMPORT:
34+
return "Import";
35+
case CHANNEL_EXPORT:
36+
return "Export";
37+
default:
38+
throw new IllegalArgumentException("Unknown notification channel");
39+
}
40+
}
41+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

app/src/main/java/protect/card_locker/importexport/MultiFormatImporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public static ImportExportResult importData(Context context, SQLiteDatabase data
4545
break;
4646
}
4747

48-
String error = null;
48+
String error;
4949
if (importer != null) {
5050
File inputFile;
5151
try {

0 commit comments

Comments
 (0)