1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/*
* Copyright (C) 2012 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.explorer.ui.dialogs;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.HapticFeedbackConstants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.cyanogenmod.explorer.R;
import com.cyanogenmod.explorer.adapters.TwoColumnsMenuListAdapter;
import com.cyanogenmod.explorer.listeners.OnRequestRefreshListener;
import com.cyanogenmod.explorer.listeners.OnSelectionListener;
import com.cyanogenmod.explorer.model.FileSystemObject;
import com.cyanogenmod.explorer.ui.policy.ActionsPolicy;
import com.cyanogenmod.explorer.ui.policy.ActionsPolicy.LinkedResource;
import com.cyanogenmod.explorer.util.DialogHelper;
import com.cyanogenmod.explorer.util.FileHelper;
import com.cyanogenmod.explorer.util.SelectionHelper;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* A class that wraps a dialog for showing the list of actions that
* the user can do.
*/
public class ActionsDialog implements OnItemClickListener, OnItemLongClickListener {
/**
* @hide
*/
final Context mContext;
private final boolean mGlobal;
/**
* @hide
*/
AlertDialog mDialog;
private ListView mListView;
/**
* @hide
*/
final FileSystemObject mFso;
/**
* @hide
*/
OnRequestRefreshListener mOnRequestRefreshListener;
/**
* @hide
*/
OnSelectionListener mOnSelectionListener;
/**
* Constructor of <code>ActionsDialog</code>.
*
* @param context The current context
* @param fso The file system object associated
* @param global If the menu to display will be the global one (Global actions)
*/
public ActionsDialog(Context context, FileSystemObject fso, boolean global) {
super();
//Save the data
this.mFso = fso;
this.mContext = context;
this.mGlobal = global;
//Initialize dialog
init(context, global ? R.id.mnu_actions_global : R.id.mnu_actions_fso);
}
/**
* Method that initializes the dialog.
*
* @param context The current context
* @param group The group of action menus to show
*/
private void init(Context context, int group) {
//Create the menu adapter
TwoColumnsMenuListAdapter adapter =
new TwoColumnsMenuListAdapter(context, R.menu.actions, group);
adapter.setOnItemClickListener(this);
adapter.setOnItemLongClickListener(this);
//Create the list view
this.mListView = new ListView(context);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
this.mListView.setLayoutParams(params);
this.mListView.setAdapter(adapter);
//Create the dialog
this.mDialog = DialogHelper.createDialog(
context,
R.drawable.ic_holo_light_actions,
R.string.actions_dialog_title,
this.mListView);
}
/**
* Method that sets the listener for communicate a refresh request.
*
* @param onRequestRefreshListener The request refresh listener
*/
public void setOnRequestRefreshListener(OnRequestRefreshListener onRequestRefreshListener) {
this.mOnRequestRefreshListener = onRequestRefreshListener;
}
/**
* Method that sets the listener for requesting selection data
*
* @param onSelectionListener The request selection data listener
*/
public void setOnSelectionListener(OnSelectionListener onSelectionListener) {
this.mOnSelectionListener = onSelectionListener;
}
/**
* Method that shows the dialog.
*/
public void show() {
TwoColumnsMenuListAdapter adapter =
(TwoColumnsMenuListAdapter)this.mListView.getAdapter();
configureMenu(adapter.getMenu());
this.mDialog.show();
}
/**
* {@inheritDoc}
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, final long id) {
//Retrieve the menu item
MenuItem menuItem = ((TwoColumnsMenuListAdapter)parent.getAdapter()).getItemById((int)id);
//What action was selected?
switch ((int)id) {
//- Create new object
case R.id.mnu_actions_new_directory:
case R.id.mnu_actions_new_file:
// Dialog is dismissed inside showInputNameDialog
if (this.mOnSelectionListener != null) {
showInputNameDialog(menuItem);
return;
}
break;
//- Rename
case R.id.mnu_actions_rename:
// Dialog is dismissed inside showInputNameDialog
if (this.mOnSelectionListener != null) {
showFsoInputNameDialog(menuItem, this.mFso);
return;
}
break;
//- Delete
case R.id.mnu_actions_delete:
ActionsPolicy.removeFileSystemObject(
this.mContext,
this.mFso,
this.mOnSelectionListener,
this.mOnRequestRefreshListener);
break;
//- Refresh
case R.id.mnu_actions_refresh:
if (this.mOnRequestRefreshListener != null) {
this.mOnRequestRefreshListener.onRequestRefresh(null); //Refresh all
}
break;
//- Select/Deselect
case R.id.mnu_actions_select:
case R.id.mnu_actions_deselect:
if (this.mOnSelectionListener != null) {
this.mOnSelectionListener.onToggleSelection(this.mFso);
}
break;
case R.id.mnu_actions_select_all:
if (this.mOnSelectionListener != null) {
this.mOnSelectionListener.onSelectAllVisibleItems();
}
break;
case R.id.mnu_actions_deselect_all:
if (this.mOnSelectionListener != null) {
this.mOnSelectionListener.onDeselectAllVisibleItems();
}
break;
//- Open
case R.id.mnu_actions_open:
ActionsPolicy.openFileSystemObject(this.mContext, this.mFso, false);
break;
//- Open with
case R.id.mnu_actions_open_with:
ActionsPolicy.openFileSystemObject(this.mContext, this.mFso, true);
break;
//- Send
case R.id.mnu_actions_send:
ActionsPolicy.sendFileSystemObject(this.mContext, this.mFso);
break;
// Paste selection
case R.id.mnu_actions_paste_selection:
if (this.mOnSelectionListener != null) {
List<FileSystemObject> selection =
this.mOnSelectionListener.onRequestSelectedFiles();
ActionsPolicy.copyFileSystemObjects(
this.mContext,
createLinkedResource(selection, this.mFso),
this.mOnSelectionListener,
this.mOnRequestRefreshListener);
}
break;
// Move selection
case R.id.mnu_actions_move_selection:
if (this.mOnSelectionListener != null) {
List<FileSystemObject> selection =
this.mOnSelectionListener.onRequestSelectedFiles();
ActionsPolicy.moveFileSystemObjects(
this.mContext,
createLinkedResource(selection, this.mFso),
this.mOnSelectionListener,
this.mOnRequestRefreshListener);
}
break;
// Delete selection
case R.id.mnu_actions_delete_selection:
if (this.mOnSelectionListener != null) {
List<FileSystemObject> selection =
this.mOnSelectionListener.onRequestSelectedFiles();
ActionsPolicy.removeFileSystemObjects(
this.mContext,
selection,
this.mOnSelectionListener,
this.mOnRequestRefreshListener);
}
break;
//- Create copy
case R.id.mnu_actions_create_copy:
// Create a copy of the fso
if (this.mOnSelectionListener != null) {
ActionsPolicy.createCopyFileSystemObject(
this.mContext,
this.mFso,
this.mOnSelectionListener,
this.mOnRequestRefreshListener);
}
break;
//- Add to bookmarks
case R.id.mnu_actions_add_to_bookmarks:
case R.id.mnu_actions_add_to_bookmarks_current_folder:
ActionsPolicy.addToBookmarks(this.mContext, this.mFso);
break;
//- Properties
case R.id.mnu_actions_properties:
case R.id.mnu_actions_properties_current_folder:
ActionsPolicy.showPropertiesDialog(
this.mContext, this.mFso, this.mOnRequestRefreshListener);
break;
default:
break;
}
//Dismiss the dialog
this.mDialog.dismiss();
}
/**
* Method that show a new dialog for input a name.
*
* @param menuItem The item menu associated
*/
private void showInputNameDialog(final MenuItem menuItem) {
//Hide the dialog
this.mDialog.hide();
//Show the input name dialog
final InputNameDialog inputNameDialog =
new InputNameDialog(
this.mContext,
this.mOnSelectionListener.onRequestCurrentItems(),
menuItem.getTitle().toString());
inputNameDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//Show the menu again
ActionsDialog.this.mDialog.show();
}
});
inputNameDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
//Retrieve the name an execute the action
try {
String name = inputNameDialog.getName();
createNewFileSystemObject(menuItem.getItemId(), name);
} finally {
ActionsDialog.this.mDialog.dismiss();
}
}
});
inputNameDialog.show();
}
/**
* Method that show a new dialog for input a name for an existing fso.
*
* @param menuItem The item menu associated
* @param fso The file system object
*/
private void showFsoInputNameDialog(final MenuItem menuItem, final FileSystemObject fso) {
//Hide the dialog
this.mDialog.hide();
//Show the input name dialog
final InputNameDialog inputNameDialog =
new InputNameDialog(
this.mContext,
this.mOnSelectionListener.onRequestCurrentItems(),
fso,
menuItem.getTitle().toString());
inputNameDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//Show the menu again
ActionsDialog.this.mDialog.show();
}
});
inputNameDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
//Retrieve the name an execute the action
try {
String name = inputNameDialog.getName();
switch (menuItem.getItemId()) {
case R.id.mnu_actions_rename:
// Rename the fso
if (ActionsDialog.this.mOnSelectionListener != null) {
ActionsPolicy.renameFileSystemObject(
ActionsDialog.this.mContext,
ActionsDialog.this.mFso,
name,
ActionsDialog.this.mOnSelectionListener,
ActionsDialog.this.mOnRequestRefreshListener);
}
break;
default:
break;
}
} finally {
ActionsDialog.this.mDialog.dismiss();
}
}
});
inputNameDialog.show();
}
/**
* {@inheritDoc}
*/
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
view.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
DialogHelper.showToast(
this.mContext,
((TextView)view).getText().toString(),
Toast.LENGTH_SHORT);
return true;
}
/**
* Method that create the a new file system object.
*
* @param menuId The menu identifier (need to determine the fso type)
* @param name The name of the file system object
* @hide
*/
void createNewFileSystemObject(final int menuId, final String name) {
switch (menuId) {
case R.id.mnu_actions_new_directory:
ActionsPolicy.createNewDirectory(
this.mContext, name,
this.mOnSelectionListener, this.mOnRequestRefreshListener);
break;
case R.id.mnu_actions_new_file:
ActionsPolicy.createNewFile(
this.mContext, name,
this.mOnSelectionListener, this.mOnRequestRefreshListener);
break;
default:
break;
}
}
/**
* Method that configure the menu to show according the actual information,
* the kind of request, the file selection, the mount point, ...
*
* @param menu The menu to configure
*/
private void configureMenu(Menu menu) {
// Check actions that needs a valid reference
if (!this.mGlobal && this.mFso != null) {
//- Select/Deselect -> Only one of them
if (this.mOnSelectionListener != null) {
boolean selected =
SelectionHelper.isFileSystemObjectSelected(
this.mOnSelectionListener.onRequestSelectedFiles(),
this.mFso);
menu.removeItem(selected ? R.id.mnu_actions_select : R.id.mnu_actions_deselect);
} else {
// Remove both menus
menu.removeItem(R.id.mnu_actions_select);
menu.removeItem(R.id.mnu_actions_deselect);
}
//- Open/Open with -> Only when the fso is not a folder and is not a system file
if (FileHelper.isDirectory(this.mFso) || FileHelper.isSystemFile(this.mFso)) {
menu.removeItem(R.id.mnu_actions_open);
menu.removeItem(R.id.mnu_actions_open_with);
menu.removeItem(R.id.mnu_actions_send);
}
}
//- Add to bookmarks -> Only directories
if (this.mFso != null && FileHelper.isRootDirectory(this.mFso)) {
menu.removeItem(R.id.mnu_actions_add_to_bookmarks);
menu.removeItem(R.id.mnu_actions_add_to_bookmarks_current_folder);
}
// Paste/Move only when have a selection
if (this.mGlobal) {
List<FileSystemObject> selection = null;
if (this.mOnSelectionListener != null) {
selection = this.mOnSelectionListener.onRequestSelectedFiles();
}
if (selection == null || selection.size() == 0 ||
(this.mFso != null && !FileHelper.isDirectory(this.mFso))) {
// Remove paste/move actions
menu.removeItem(R.id.mnu_actions_paste_selection);
menu.removeItem(R.id.mnu_actions_move_selection);
menu.removeItem(R.id.mnu_actions_delete_selection);
}
}
}
/**
* Method that creates a {@link LinkedResource} for the list of object to the
* destination directory
*
* @param items The list of the source items
* @param directory The destination directory
*/
private static List<LinkedResource> createLinkedResource(
List<FileSystemObject> items, FileSystemObject directory) {
List<LinkedResource> resources =
new ArrayList<ActionsPolicy.LinkedResource>(items.size());
for (int i = 0; i < items.size(); i++) {
FileSystemObject fso = items.get(i);
File src = new File(fso.getFullPath());
File dst = new File(directory.getFullPath(), fso.getName());
resources.add(new LinkedResource(src, dst));
}
return resources;
}
}
|