Skip to content

Commit ed835c4

Browse files
committed
minor
- Android: auto-saved playlists could be lost sometimes
1 parent 6c73683 commit ed835c4

File tree

15 files changed

+173
-36
lines changed

15 files changed

+173
-36
lines changed

android/phiola/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.github.stsaz.phiola"
99
minSdkVersion 16
1010
targetSdk 33
11-
versionCode 20507
12-
versionName '2.5-rc7'
11+
versionCode 20508
12+
versionName '2.5.8'
1313
}
1414

1515
buildFeatures {

android/phiola/src/main/java/com/github/stsaz/phiola/AboutActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
3434

3535
@Override
3636
protected void onDestroy() {
37+
core.unref();
3738
super.onDestroy();
3839
}
3940

android/phiola/src/main/java/com/github/stsaz/phiola/ConvertActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ protected void onCreate(Bundle savedInstanceState) {
2626
b = ConvertBinding.inflate(getLayoutInflater());
2727
setContentView(b.getRoot());
2828

29+
core = Core.getInstance();
2930
varmenu = new VarMenu(this);
30-
explorer = new ExplorerMenu(this);
31+
explorer = new ExplorerMenu(core, this);
3132
b.eOutDir.setOnClickListener(v -> explorer.show(b.eOutDir, 0));
3233
b.eOutName.setOnClickListener(v -> varmenu.show(b.eOutName, new String[]{
3334
"@filename", "@album", "@artist", "@title", "@tracknumber",
@@ -97,7 +98,6 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
9798

9899
b.bStart.setOnClickListener((v) -> convert());
99100

100-
core = Core.getInstance();
101101
load();
102102

103103
String iname = getIntent().getStringExtra("iname");

android/phiola/src/main/java/com/github/stsaz/phiola/ExplorerMenu.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class ExplorerMenu {
2121

2222
static final int F_MULTI = 1;
2323

24-
ExplorerMenu(Activity parent) {
25-
core = Core.getInstance();
24+
ExplorerMenu(Core core, Activity parent) {
25+
this.core = core;
2626
this.parent = parent;
2727
}
2828

android/phiola/src/main/java/com/github/stsaz/phiola/ListSaveActivity.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ protected void onCreate(Bundle savedInstanceState) {
2222
b = ListSaveBinding.inflate(getLayoutInflater());
2323
setContentView(b.getRoot());
2424

25-
explorer = new ExplorerMenu(this);
25+
core = Core.getInstance();
26+
explorer = new ExplorerMenu(core, this);
2627

2728
b.bSave.setOnClickListener((v) -> save());
2829
b.eDir.setOnClickListener(v -> explorer.show(b.eDir, 0));
2930

30-
core = Core.getInstance();
3131
load();
3232

3333
String name = getIntent().getStringExtra("name");
@@ -60,8 +60,11 @@ private void save() {
6060
}
6161
save2(fn);
6262
}
63-
private void save2(String fn) {
64-
if (!core.queue().current_save(fn)) {
63+
64+
private void save2(String fn) { core.queue().current_save(fn, this::save_complete); }
65+
66+
private void save_complete(int status) {
67+
if (status != 0) {
6568
core.errlog(TAG, "Error saving playlist file");
6669
return;
6770
}

android/phiola/src/main/java/com/github/stsaz/phiola/MainActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ public boolean onQueryTextChange(String newText) {
476476
});
477477

478478
b.list.setLayoutManager(new LinearLayoutManager(this));
479-
pl_adapter = new PlaylistAdapter(this, new PlaylistViewHolder.Parent() {
479+
pl_adapter = new PlaylistAdapter(core, this, new PlaylistViewHolder.Parent() {
480480

481481
public int count() {
482482
if (gui.view == GUI.V_EXPLORER)
@@ -563,7 +563,6 @@ private void rec_state_set(boolean active) {
563563
}
564564

565565
private void rec_fin(int code, String filename) {
566-
rec_state_set(false);
567566
stopService(new Intent(this, RecSvc.class));
568567
if (code == 0) {
569568
if (core.setts.rec_list_add)
@@ -577,6 +576,7 @@ private void rec_fin(int code, String filename) {
577576

578577
private void rec_stop() {
579578
state(GUI.STATE_RECORDING | GUI.STATE_REC_PAUSED, 0);
579+
rec_state_set(false);
580580
String e = track.record_stop();
581581
if (e != null)
582582
core.errlog(TAG, String.format("%s: %s", getString(R.string.main_rec_err), e));

android/phiola/src/main/java/com/github/stsaz/phiola/Phiola.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ interface RecordCallback {
125125

126126
interface QueueCallback {
127127
void on_change(long q, int flags, int pos);
128+
void on_complete(int operation, int status);
128129
}
129130
native void quSetCallback(QueueCallback cb);
130131

@@ -206,5 +207,5 @@ to the index within its parent (not filtered) list */
206207
/** Load playlist from a file on disk */
207208
native int quLoad(long q, String filepath);
208209

209-
native boolean quSave(long q, String filepath);
210+
native void quSave(long q, String filepath);
210211
}

android/phiola/src/main/java/com/github/stsaz/phiola/PlaylistAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class PlaylistAdapter extends RecyclerView.Adapter<PlaylistViewHolder> {
5252
private final LayoutInflater inflater;
5353
private final PlaylistViewHolder.Parent parent;
5454

55-
PlaylistAdapter(Context ctx, PlaylistViewHolder.Parent parent) {
56-
core = Core.getInstance();
55+
PlaylistAdapter(Core core, Context ctx, PlaylistViewHolder.Parent parent) {
56+
this.core = core;
5757
queue = core.queue();
5858
inflater = LayoutInflater.from(ctx);
5959
this.parent = parent;

android/phiola/src/main/java/com/github/stsaz/phiola/Queue.java

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
package com.github.stsaz.phiola;
55

6+
import android.os.ConditionVariable;
7+
68
import java.io.File;
79
import java.util.ArrayList;
810
import java.util.Collections;
@@ -94,12 +96,10 @@ void load_once() {
9496
phi.quLoad(q, file_name);
9597
file_name = null;
9698
}
99+
boolean loaded() { return (file_name == null); }
97100

98-
boolean save(String filepath) {
99-
boolean b = phi.quSave(q, filepath);
100-
if (b)
101-
modified = false;
102-
return b;
101+
void save(String filepath) {
102+
phi.quSave(q, filepath);
103103
}
104104
}
105105

@@ -222,7 +222,7 @@ public int open(TrackHandle t) {
222222
nfy = new ArrayList<>();
223223
}
224224

225-
private void on_change(long q, int flags, int pos) {
225+
private void q_on_change(long q, int flags, int pos) {
226226
core.tq.post(() -> {
227227
if (queues.size() == 0)
228228
return;
@@ -264,6 +264,17 @@ else if (pos < trk_idx)
264264
});
265265
}
266266

267+
private void q_on_complete(int operation, int status) {
268+
core.dbglog(TAG, "q_on_complete: %d %d", operation, status);
269+
270+
if (save_on_close_event())
271+
return;
272+
273+
core.tq.post(() -> {
274+
save_complete(status);
275+
});
276+
}
277+
267278
int new_list() {
268279
filter_close();
269280
queues.add(new PhiolaQueue(core.phiola, 0, 0));
@@ -355,29 +366,99 @@ void load() {
355366
if (v != 0)
356367
phi.quConf(v, v);
357368

358-
core.phiola.quSetCallback(this::on_change);
369+
core.phiola.quSetCallback(new Phiola.QueueCallback() {
370+
public void on_change(long q, int flags, int pos) { q_on_change(q, flags, pos); }
371+
public void on_complete(int operation, int status) { q_on_complete(operation, status); }
372+
});
373+
}
374+
375+
private ConditionVariable save_event;
376+
private int save_pending;
377+
378+
private boolean save_on_close_event() {
379+
if (save_event != null) {
380+
if (--save_pending == 0)
381+
save_event.open();
382+
return true;
383+
}
384+
return false;
359385
}
360386

361387
void saveconf() {
362388
core.dir_make(core.setts.pub_data_dir);
389+
390+
save_pending = 0;
391+
for (PhiolaQueue q : queues) {
392+
if (q.conversion)
393+
continue;
394+
395+
if (q.modified && q.loaded())
396+
save_pending++;
397+
}
398+
399+
if (save_pending != 0)
400+
save_event = new ConditionVariable();
401+
363402
int i = 0;
364403
for (PhiolaQueue q : queues) {
365404
if (q.conversion)
366405
continue;
367-
if (q.modified)
368-
q.save(list_file_name(i));
406+
407+
if (q.modified) {
408+
if (q.loaded()) {
409+
q.save(list_file_name(i));
410+
411+
} else {
412+
// The playlist is already on disk: rename file so it won't conflict with the existing one
413+
core.file_rename(q.file_name, list_file_name(i) + ".tmp");
414+
}
415+
}
416+
369417
i++;
370418
}
371419

372420
core.file_delete(list_file_name(i));
421+
422+
// Finish file-rename procedure
423+
i = 0;
424+
for (PhiolaQueue q : queues) {
425+
if (q.conversion)
426+
continue;
427+
428+
if (q.modified && !q.loaded()) {
429+
String s = list_file_name(i);
430+
core.file_rename(s + ".tmp", s);
431+
}
432+
433+
i++;
434+
}
435+
436+
// Wait until the modified playlists are stored on disk
437+
if (save_event != null) {
438+
save_event.block();
439+
save_event = null;
440+
}
441+
442+
for (PhiolaQueue q : queues) {
443+
q.modified = false;
444+
}
445+
}
446+
447+
interface QuSaveCallback {
448+
void on_complete(int status);
449+
}
450+
private QuSaveCallback q_save_done;
451+
private void save_complete(int status) {
452+
if (q_save_done != null) {
453+
q_save_done.on_complete(status);
454+
q_save_done = null;
455+
}
373456
}
374457

375458
/** Save playlist to a file */
376-
boolean current_save(String fn) {
377-
if (!q_selected().save(fn))
378-
return false;
379-
core.dbglog(TAG, "saved %s", fn);
380-
return true;
459+
void current_save(String fn, QuSaveCallback on_complete) {
460+
q_save_done = on_complete;
461+
q_selected().save(fn);
381462
}
382463

383464
/** Get next list index (round-robin) */

android/phiola/src/main/java/com/github/stsaz/phiola/SettingsActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ protected void onCreate(Bundle savedInstanceState) {
3030
if (actionBar != null)
3131
actionBar.setDisplayHomeAsUpEnabled(true);
3232

33-
explorer = new ExplorerMenu(this);
33+
core = Core.getInstance();
34+
explorer = new ExplorerMenu(core, this);
3435

3536
play_init();
3637

@@ -39,7 +40,6 @@ protected void onCreate(Bundle savedInstanceState) {
3940

4041
rec_init();
4142

42-
core = Core.getInstance();
4343
load();
4444
}
4545

0 commit comments

Comments
 (0)