Skip to content

Commit 34548b0

Browse files
committed
minor
+ Android: new command `Rename File`
1 parent bc6ee07 commit 34548b0

File tree

11 files changed

+237
-102
lines changed

11 files changed

+237
-102
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 20506
12-
versionName '2.5-rc6'
11+
versionCode 20507
12+
versionName '2.5-rc7'
1313
}
1414

1515
buildFeatures {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,14 @@ static Core init_once(Context ctx) {
4848
PUB_DATA_DIR = "phiola-dbg";
4949
Core c = new Core();
5050
c.refcount = 1;
51-
if (0 != c.init(ctx))
52-
return null;
51+
c.init(ctx);
5352
instance = c;
5453
return c;
5554
}
5655
return getInstance();
5756
}
5857

59-
private int init(@NonNull Context ctx) {
58+
private void init(@NonNull Context ctx) {
6059
dbglog(TAG, "init");
6160
context = ctx;
6261
work_dir = ctx.getFilesDir().getPath();
@@ -78,7 +77,6 @@ private int init(@NonNull Context ctx) {
7877
loadconf();
7978
qu.load();
8079
gui.lists_number(qu.number());
81-
return 0;
8280
}
8381

8482
void unref() {

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

Lines changed: 98 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public class MainActivity extends AppCompatActivity {
5252
protected void onCreate(Bundle savedInstanceState) {
5353
super.onCreate(savedInstanceState);
5454

55-
if (0 != init_mods())
56-
return;
55+
init_mods();
5756
b = MainBinding.inflate(getLayoutInflater());
5857
init_ui();
5958
init_system();
@@ -123,37 +122,41 @@ public boolean onCreateOptionsMenu(Menu menu) {
123122

124123
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
125124
switch (item.getItemId()) {
126-
case R.id.action_settings:
127-
startActivity(new Intent(this, SettingsActivity.class));
128-
return true;
125+
case R.id.action_settings:
126+
startActivity(new Intent(this, SettingsActivity.class));
127+
break;
129128

130-
case R.id.action_play_auto_stop:
131-
play_auto_stop();
132-
return true;
129+
case R.id.action_play_auto_stop:
130+
play_auto_stop();
131+
break;
133132

134-
case R.id.action_file_menu_show:
135-
if (mfile == null) {
136-
mfile = new PopupMenu(this, findViewById(R.id.action_file_menu_show));
137-
mfile.getMenuInflater().inflate(R.menu.file, mfile.getMenu());
138-
mfile.setOnMenuItemClickListener(this::file_menu_click);
139-
}
140-
mfile.show();
141-
return true;
133+
case R.id.action_file_menu_show:
134+
if (mfile == null) {
135+
mfile = new PopupMenu(this, findViewById(R.id.action_file_menu_show));
136+
mfile.getMenuInflater().inflate(R.menu.file, mfile.getMenu());
137+
mfile.setOnMenuItemClickListener(this::file_menu_click);
138+
}
139+
mfile.show();
140+
break;
142141

143-
case R.id.action_list_menu_show:
144-
if (mlist == null) {
145-
mlist = new PopupMenu(this, findViewById(R.id.action_list_menu_show));
146-
mlist.getMenuInflater().inflate(R.menu.list, mlist.getMenu());
147-
mlist.setOnMenuItemClickListener(this::list_menu_click);
148-
}
149-
mlist.show();
150-
return true;
142+
case R.id.action_list_menu_show:
143+
if (mlist == null) {
144+
mlist = new PopupMenu(this, findViewById(R.id.action_list_menu_show));
145+
mlist.getMenuInflater().inflate(R.menu.list, mlist.getMenu());
146+
mlist.setOnMenuItemClickListener(this::list_menu_click);
147+
}
148+
mlist.show();
149+
break;
151150

152-
case R.id.action_about:
153-
startActivity(new Intent(this, AboutActivity.class));
154-
return true;
151+
case R.id.action_about:
152+
startActivity(new Intent(this, AboutActivity.class));
153+
break;
154+
155+
default:
156+
return super.onOptionsItemSelected(item);
155157
}
156-
return super.onOptionsItemSelected(item);
158+
159+
return true;
157160
}
158161

159162
private boolean file_menu_click(MenuItem item) {
@@ -245,19 +248,27 @@ private boolean list_menu_click(MenuItem item) {
245248
return true;
246249
}
247250

251+
private PopupMenu mlist_sort;
252+
private static final int[] mlist_sort_data = {
253+
Phiola.QU_SORT_FILENAME, R.string.mlist_sort_filename,
254+
Phiola.QU_SORT_FILESIZE, R.string.mlist_sort_filesize,
255+
Phiola.QU_SORT_FILEDATE, R.string.mlist_sort_filedate,
256+
Phiola.QU_SORT_RANDOM, R.string.mlist_shuffle,
257+
Phiola.QU_SORT_TAG_ARTIST, R.string.mlist_sort_tag_artist,
258+
Phiola.QU_SORT_TAG_DATE, R.string.mlist_sort_tag_date,
259+
};
248260
private void list_sort_menu_show() {
249-
PopupMenu m = new PopupMenu(this, b.list);
250-
m.setOnMenuItemClickListener((item) -> {
251-
queue.current_sort(item.getItemId());
252-
return true;
253-
});
254-
m.getMenu().add(0, Phiola.QU_SORT_FILENAME, 0, getString(R.string.mlist_sort_filename));
255-
m.getMenu().add(0, Phiola.QU_SORT_FILESIZE, 0, getString(R.string.mlist_sort_filesize));
256-
m.getMenu().add(0, Phiola.QU_SORT_FILEDATE, 0, getString(R.string.mlist_sort_filedate));
257-
m.getMenu().add(0, Phiola.QU_SORT_RANDOM, 0, getString(R.string.mlist_shuffle));
258-
m.getMenu().add(0, Phiola.QU_SORT_TAG_ARTIST, 0, getString(R.string.mlist_sort_tag_artist));
259-
m.getMenu().add(0, Phiola.QU_SORT_TAG_DATE, 0, getString(R.string.mlist_sort_tag_date));
260-
m.show();
261+
if (mlist_sort == null) {
262+
mlist_sort = new PopupMenu(this, b.list);
263+
mlist_sort.setOnMenuItemClickListener((item) -> {
264+
queue.current_sort(item.getItemId());
265+
return true;
266+
});
267+
for (int i = 0; i < mlist_sort_data.length; i += 2) {
268+
mlist_sort.getMenu().add(0, mlist_sort_data[i], 0, getString(mlist_sort_data[i+1]));
269+
}
270+
}
271+
mlist_sort.show();
261272
}
262273

263274
private boolean item_menu_click(MenuItem item) {
@@ -274,7 +285,7 @@ private boolean item_menu_click(MenuItem item) {
274285
case R.id.action_file_delete:
275286
fn = queue.visible_url(item_menu_qi);
276287
if (!fn.isEmpty())
277-
file_delete_ask(item_menu_qi, fn);
288+
file_delete_ask(FD_VISIBLE, item_menu_qi, fn);
278289
break;
279290

280291
case R.id.action_file_move:
@@ -283,6 +294,15 @@ private boolean item_menu_click(MenuItem item) {
283294
file_move(fn, item_menu_qi);
284295
break;
285296

297+
case R.id.action_file_rename:
298+
fn = queue.visible_url(item_menu_qi);
299+
if (!fn.isEmpty())
300+
file_rename_ask(fn, item_menu_qi);
301+
break;
302+
303+
case R.id.action_file_show_info:
304+
file_tags_show_sel(item_menu_qi); break;
305+
286306
case R.id.action_list_next_add_sel:
287307
fn = queue.visible_url(item_menu_qi);
288308
if (!fn.isEmpty())
@@ -355,10 +375,8 @@ private boolean user_ask_record() {
355375
}
356376

357377
/** Initialize core and modules */
358-
private int init_mods() {
378+
private void init_mods() {
359379
core = Core.init_once(getApplicationContext());
360-
if (core == null)
361-
return -1;
362380
core.dbglog(TAG, "init_mods()");
363381

364382
gui = core.gui();
@@ -379,7 +397,6 @@ public void on_change(int how, int pos) {
379397
track.observer_add(trk_nfy);
380398
trackctl = new TrackCtl(core, this);
381399
trackctl.connect();
382-
return 0;
383400
}
384401

385402
/** Set UI objects and register event handlers */
@@ -671,9 +688,13 @@ private void playlist_menu_show() {
671688
}
672689

673690
private void file_tags_show() { startActivity(new Intent(this, TagsActivity.class)); }
691+
private void file_tags_show_sel(int pos) {
692+
startActivity(new Intent(this, TagsActivity.class)
693+
.putExtra("pos", pos));
694+
}
674695

675696
/** Delete file and update view */
676-
private void file_del(int pos, String fn) {
697+
private void file_del(int how, int pos, String fn) {
677698
if (!core.setts.file_del) {
678699
String e = core.util.trash(core.setts.trash_dir, fn);
679700
if (!e.isEmpty()) {
@@ -687,21 +708,24 @@ private void file_del(int pos, String fn) {
687708
gui.msg_show(this, "Deleted file");
688709
}
689710

690-
plist_filter_clear();
691-
queue.active_remove(pos);
711+
if (how == FD_ACTIVE)
712+
queue.active_remove(pos);
713+
else
714+
queue.current_remove(pos);
692715
}
693716

694717
private void file_del_cur() {
695718
int pos = queue.active_pos();
696719
String fn = queue.active_track_url();
697720
if (fn != null)
698-
file_delete_ask(pos, fn);
721+
file_delete_ask(FD_ACTIVE, pos, fn);
699722
}
700723

701-
private void file_delete_ask(int pos, String fn) {
702-
AlertDialog.Builder b = new AlertDialog.Builder(this);
703-
b.setIcon(android.R.drawable.ic_dialog_alert);
704-
b.setTitle("File Delete");
724+
private static final int
725+
FD_VISIBLE = 0,
726+
FD_ACTIVE = 1;
727+
728+
private void file_delete_ask(int how, int pos, String fn) {
705729
String msg, btn;
706730
if (core.setts.file_del) {
707731
msg = String.format("Delete file from storage: %s ?", fn);
@@ -710,10 +734,9 @@ private void file_delete_ask(int pos, String fn) {
710734
msg = String.format("Move file to Trash: %s ?", fn);
711735
btn = "Trash";
712736
}
713-
b.setMessage(msg);
714-
b.setPositiveButton(btn, (dialog, which) -> file_del(pos, fn));
715-
b.setNegativeButton("Cancel", null);
716-
b.show();
737+
GUI.dlg_question(this, "File Delete", msg
738+
, btn, "Cancel"
739+
, (dialog, which) -> file_del(how, pos, fn));
717740
}
718741

719742
private void file_move(String fn, int pos) {
@@ -738,6 +761,22 @@ private void file_move_confirmed(String fn, int pos) {
738761
gui.msg_show(this, "Moved file to %s", gui.cur_path);
739762
}
740763

764+
private void file_rename_ask(String fn, int pos) {
765+
gui.dlg_edit(this, "Rename file"
766+
, "Specify new name", Util.path_split3(fn)[1]
767+
, "Rename", "Cancel"
768+
, (new_text) -> { file_rename_confirmed(pos, new_text); }
769+
);
770+
}
771+
772+
private void file_rename_confirmed(int pos, String name) {
773+
if (!queue.visible_track_rename(pos, name)) {
774+
core.errlog(TAG, "file rename: ERROR");
775+
return;
776+
}
777+
gui.msg_show(this, "Renamed file");
778+
}
779+
741780
private void plist_open_new(String fn) {
742781
list_new();
743782

@@ -828,7 +867,8 @@ void list_leave() {
828867
if (gui.view == GUI.V_PLAYLIST) {
829868
queue.current_filter("");
830869
gui.list_filter = b.tfilter.getQuery().toString();
831-
gui.list_scroll_pos_set(queue.current_index(), pos);
870+
if (gui.list_filter.isEmpty())
871+
gui.list_scroll_pos_set(queue.current_index(), pos);
832872
} else if (gui.view == GUI.V_LIBRARY) {
833873
gui.mlib_scroll_pos = pos;
834874
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ to the index within its parent (not filtered) list */
188188
<0 if some files were not moved. */
189189
native int quMoveAll(long q, String dst_dir);
190190

191-
/** Move file to the specified directory. */
191+
static final int
192+
QR_MOVE = 0, // move to the specified directory
193+
QR_RENAME = 1; // rename file
194+
/** Move file */
192195
native int quRename(long q, int pos, String target, int flags);
193196

194197
static final int

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,9 @@ private void play_on_close(TrackHandle t) {
510510
}
511511

512512
String active_track_url() {
513-
if (i_selected != i_active
514-
|| trk_idx < 0)
513+
if (trk_idx < 0)
515514
return null;
516-
return phi.quEntry(queues.get(i_selected).q, trk_idx);
515+
return phi.quEntry(queues.get(i_active).q, trk_idx);
517516
}
518517

519518
String visible_url(int i) {
@@ -538,7 +537,11 @@ boolean active_track_move(int pos, String target_dir) {
538537
}
539538

540539
boolean visible_track_move(int pos, String target_dir) {
541-
return 0 == phi.quRename(q_visible().q, pos, target_dir, 0);
540+
return 0 == phi.quRename(q_visible().q, pos, target_dir, Phiola.QR_MOVE);
541+
}
542+
543+
boolean visible_track_rename(int pos, String name) {
544+
return 0 == phi.quRename(q_visible().q, pos, name, Phiola.QR_RENAME);
542545
}
543546

544547
void current_clear() {
@@ -550,9 +553,12 @@ void current_clear() {
550553
}
551554

552555
void current_remove(int pos) {
553-
if (q_filtered != null)
554-
return;
555-
queues.get(i_selected).remove(pos);
556+
int i = pos;
557+
if (q_filtered != null) {
558+
i = phi.quCmd(q_visible().q, Phiola.QUCOM_INDEX, pos);
559+
q_filtered.remove(pos);
560+
}
561+
queues.get(i_selected).remove(i);
556562
}
557563

558564
void current_remove_non_existing() {
@@ -668,6 +674,13 @@ String[] active_meta() {
668674
return m.meta;
669675
}
670676

677+
String[] visible_meta(int pos) {
678+
Phiola.Meta m = phi.quMeta(q_visible().q, pos);
679+
if (m == null)
680+
return null;
681+
return m.meta;
682+
}
683+
671684
void current_sort(int flags) {
672685
if (q_filtered != null) return;
673686

0 commit comments

Comments
 (0)