OSDN Git Service

T29623
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / EverFolderActivity.java
1 package com.yuji.ef;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.database.sqlite.SQLiteDatabase;
12 import android.graphics.Bitmap;
13 import android.graphics.PixelFormat;
14 import android.os.Bundle;
15 import android.text.Editable;
16 import android.text.TextWatcher;
17 import android.view.Gravity;
18 import android.view.Menu;
19 import android.view.MenuItem;
20 import android.view.View;
21 import android.view.View.OnClickListener;
22 import android.view.WindowManager;
23 import android.view.inputmethod.InputMethodManager;
24 import android.widget.Button;
25 import android.widget.EditText;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28
29 import com.yuji.ef.common.CommonUtil;
30 import com.yuji.ef.common.Constant;
31 import com.yuji.ef.dao.DatabaseHelper;
32 import com.yuji.ef.dao.DirNode;
33 import com.yuji.ef.dao.FileNode;
34 import com.yuji.ef.dao.LockDao;
35 import com.yuji.ef.dao.Node;
36 import com.yuji.ef.dao.Node.Status;
37 import com.yuji.ef.dao.NodeCacheDao;
38 import com.yuji.ef.dao.NodeComparator;
39 import com.yuji.ef.dao.NodeDao;
40 import com.yuji.ef.dao.NodeDaoImpl;
41 import com.yuji.ef.dao.RootNode;
42 import com.yuji.ef.exception.EfError;
43 import com.yuji.ef.exception.EfException;
44 import com.yuji.ef.pref.EditPrefUtil;
45 import com.yuji.ef.utility.BaseActivity;
46 import com.yuji.ef.utility.BitmapCacheFactory;
47 import com.yuji.ef.utility.EvernoteIntentUtil;
48 import com.yuji.ef.utility.FolderUtil;
49 import com.yuji.ef.utility.LockHandler;
50 import com.yuji.ef.utility.LockListener;
51 import com.yuji.ef.utility.ScaleUtil;
52
53 public class EverFolderActivity extends BaseActivity implements LockListener {
54         private enum ScreenStatus {
55                 Normal, AddNode, DeleteNode, RenameNode
56         };
57
58         private EditPrefUtil pref = new EditPrefUtil(this);
59
60         private LinearLayout msgLayout;
61         private LinearLayout msgButtonLayout;
62         private Button msgOkButton;
63         private Button msgCancelButton;
64         private EditText msgEditText;
65
66         private IconFrameLayout layout;
67         private TextView confirmMsg;
68         private IconScrollView scrollView;
69         private LinearLayout buttonLayout;
70         private Button leftButton;
71         private Button rightButton;
72
73         private ScreenStatus status;
74         private NodeComparator nodeComparator = new NodeComparator();
75         private BitmapCacheFactory bitmapFactory = BitmapCacheFactory.getInstance();
76         private Node top = null;
77         private int indent = 0;
78         private int bmpPaddingLeft;
79         private int bmpWidth;
80         private int viewY;
81         private boolean lock = false;
82         private boolean isInit = true;
83         private boolean isInCycle = false;
84         private boolean isResult = false;
85         private LockHandler lockHandler = null;
86         
87         private BroadcastReceiver receiver = new BroadcastReceiver() {
88                 @Override
89                 public void onReceive(Context context, Intent intent) {
90                         String action = intent.getAction();
91
92                         if (action.equals(Constant.ACTION_MESSAGE)) {
93                                 String msg = intent
94                                                 .getStringExtra(Constant.ACTION_MESSAGE_MESSAGE);
95                                 EfException.msg(EverFolderActivity.this, msg);
96                         } else if (action.equals(Constant.ACTION_UPDATE)) {
97                                 if (isInCycle && !lock) {
98                                         LockDao lockDao = (LockDao) LockDao.getInstance();
99                                         lock = lockDao.lock(EverFolderActivity.this,
100                                                         Constant.LOCK_UPDATE_NOTE);
101                                         if (lock) {
102                                                 updateList();
103                                                 // ほんとは、setStatus()に入れたい
104                                                 layout.setVisibility(View.VISIBLE);
105                                         }
106                                         setStatus();
107                                 }
108                         }
109                 }
110         };
111
112         @Override
113         public void onCreate(Bundle savedInstanceState) {
114                 super.onCreate(savedInstanceState);
115                 setContentView(R.layout.main);
116
117                 try {
118                         IntentFilter intentFilter = new IntentFilter();
119                         intentFilter.addAction(Constant.ACTION_UPDATE);
120                         intentFilter.addAction(Constant.ACTION_MESSAGE);
121                         registerReceiver(receiver, intentFilter);
122
123                         scrollView = (IconScrollView) findViewById(R.id.scrollView);
124                         layout = (IconFrameLayout) findViewById(R.id.frameLayout);
125                         layout.setActivity(this);
126                         layout.setScrollView(scrollView);
127
128                         buttonLayout = (LinearLayout) findViewById(R.id.mainButtonLayout);
129                         leftButton = (Button) findViewById(R.id.leftButton);
130                         rightButton = (Button) findViewById(R.id.rightButton);
131
132                         msgLayout = (LinearLayout) findViewById(R.id.mainMsgLayout);
133                         confirmMsg = (TextView) findViewById(R.id.mainConfirmMsg);
134                         msgEditText = (EditText) findViewById(R.id.mainMsgEditText);
135                         msgButtonLayout = (LinearLayout) findViewById(R.id.mainMsgButtonLayout);
136                         msgOkButton = (Button) findViewById(R.id.mainMsgOkButton);
137                         msgCancelButton = (Button) findViewById(R.id.mainMsgCancelButton);
138
139                         leftButton.setOnClickListener(new OnClickListener() {
140                                 @Override
141                                 public void onClick(View v) {
142                                         leftButtonOnClick();
143                                 }
144                         });
145                         rightButton.setOnClickListener(new OnClickListener() {
146                                 @Override
147                                 public void onClick(View v) {
148                                         rightButtonOnClick();
149                                 }
150                         });
151
152                         msgEditText.addTextChangedListener(new TextWatcher() {
153                                 public void afterTextChanged(Editable s) {
154                                         if (status != ScreenStatus.DeleteNode) {
155                                                 String text = s.toString();
156                                                 msgOkButton.setEnabled(text.length() > 0);
157                                         } else {
158                                                 msgOkButton.setEnabled(true);
159                                         }
160                                 }
161
162                                 public void beforeTextChanged(CharSequence s, int start,
163                                                 int count, int after) {
164
165                                 }
166
167                                 public void onTextChanged(CharSequence s, int start,
168                                                 int before, int count) {
169
170                                 }
171                         });
172
173                         msgOkButton.setOnClickListener(new OnClickListener() {
174                                 @Override
175                                 public void onClick(View v) {
176                                         msgOkButtonOnClick(v);
177                                 }
178                         });
179                         msgCancelButton.setOnClickListener(new OnClickListener() {
180                                 @Override
181                                 public void onClick(View v) {
182                                         msgCancelOnClick(v);
183                                 }
184                         });
185
186                         ScaleUtil scaleUtil = ScaleUtil.getInstance();
187                         scaleUtil.init(this);
188
189                         bitmapFactory.init(this);
190                         Node node = new DirNode(null, null);
191                         Bitmap bmp = null;
192                         bmp = bitmapFactory.get(node.getStatusIconId());
193                         bmpPaddingLeft = bmp.getWidth();
194                         bmp = bitmapFactory.get(node.getIconId());
195                         bmpWidth = bmp.getWidth();
196
197                         float fSize = 48 * (1 / 2.0f);
198                         leftButton.setTextSize(fSize);
199                         rightButton.setTextSize(fSize);
200                 } catch (Exception e) {
201                         EfException.msg(R.string.ErrorSystem, e);
202                 }
203         }
204
205         @Override
206         public boolean onCreateOptionsMenu(Menu menu) {
207                 boolean ret = super.onCreateOptionsMenu(menu);
208                 int index = 0;
209
210                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE,
211                                 getString(R.string.MenuCut));
212                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE,
213                                 getString(R.string.MenuPaste));
214                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE,
215                                 getString(R.string.MenuAdd));
216                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE,
217                                 getString(R.string.MenuDelete));
218                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE,
219                                 getString(R.string.MenuRename));
220                 menu.add(Menu.NONE, Menu.FIRST + index++, Menu.NONE,
221                                 getString(R.string.MenuSetting));
222                 return ret;
223         }
224
225         @Override
226         public boolean onMenuOpened(int featureId, Menu menu) {
227                 boolean ret = super.onMenuOpened(featureId, menu);
228
229                 // #29304
230                 if (menu == null) {
231                         return false;
232                 }
233
234                 MenuItem item;
235
236                 boolean cutButtonFlag = false;
237                 boolean pastButtonFlag = false;
238                 boolean addButtonFlag = false;
239                 boolean deleteButtonFlag = false;
240                 boolean renameButtonFlag = false;
241                 boolean confButtonFlag = true;
242
243                 if (status == ScreenStatus.Normal) {
244                         LabelIconView target = layout.getSelectedTarget();
245                         NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
246                         long id = -1;
247                         Node node = null;
248                         if (target != null) {
249                                 id = target.getNodeId();
250                                 node = dao.searchById(id);
251                         }
252
253                         if (isDirNode(target, node, false) || isFileNode(node)) {
254                                 cutButtonFlag = true;
255                         }
256                         if (isDirNode(target, node, true) && isCut()) {
257                                 pastButtonFlag = true;
258                         }
259                         if (isDirNode(target, node, true)) {
260                                 addButtonFlag = true;
261                         }
262                         if (isDirNode(target, node, false) && !isNoteBook(node)
263                                         && CommonUtil.isNull(node.getChildrenString())) {
264                                 deleteButtonFlag = true;
265                         }
266                         if (isDirNode(target, node, false) && !isNoteBook(node)) {
267                                 renameButtonFlag = true;
268                         }
269                 }
270
271                 int index = 0;
272                 item = menu.getItem(index++);
273                 item.setVisible(cutButtonFlag);
274                 item = menu.getItem(index++);
275                 item.setVisible(pastButtonFlag);
276                 item = menu.getItem(index++);
277                 item.setVisible(addButtonFlag);
278                 item = menu.getItem(index++);
279                 item.setVisible(deleteButtonFlag);
280                 item = menu.getItem(index++);
281                 item.setVisible(renameButtonFlag);
282                 item = menu.getItem(index++);
283                 item.setVisible(confButtonFlag);
284
285                 return ret;
286         }
287
288         private boolean isFileNode(Node node) {
289                 if (node == null || !(node instanceof FileNode)) {
290                         return false;
291                 }
292                 return true;
293         }
294
295         private boolean isDirNode(LabelIconView target, Node node,
296                         boolean containRoot) {
297                 if (target == null) {
298                         return false;
299                 }
300                 if (node == null) {
301                         return false;
302                 }
303                 if (!containRoot && (node instanceof RootNode)) {
304                         return false;
305                 }
306                 if (!(node instanceof DirNode)) {
307                         return false;
308                 }
309                 return true;
310         }
311
312         private boolean isNoteBook(Node node) {
313                 if (node == null) {
314                         return false;
315                 }
316                 if (CommonUtil.isNull(node.getGuid())) {
317                         return false;
318                 }
319                 return true;
320         }
321
322         private boolean isCut() {
323                 LabelIconView srcTarget = layout.getCutTarget();
324                 if (srcTarget == null) {
325                         return false;
326                 }
327                 return true;
328         }
329
330         @Override
331         public boolean onOptionsItemSelected(MenuItem item) {
332                 Intent intent;
333                 try {
334                         switch (item.getItemId()) {
335                         case Menu.FIRST + 0:
336                                 selectMenuCut();
337                                 break;
338                         case Menu.FIRST + 1:
339                                 selectMenuPast();
340                                 break;
341                         case Menu.FIRST + 2:
342                                 selectMenuAdd();
343                                 break;
344                         case Menu.FIRST + 3:
345                                 selectMenuDelete();
346                                 break;
347                         case Menu.FIRST + 4:
348                                 selectMenuRename();
349                                 break;
350                         case Menu.FIRST + 5:
351                                 // dummy();
352                                 // dummy2();
353                                 intent = new Intent(this, (Class<?>) SettingActivity.class);
354                                 startActivityForResult(intent, 0);
355                                 break;
356                         default:
357                                 break;
358                         }
359                 } catch (Exception e) {
360                         terminate(e);
361                 }
362                 return super.onOptionsItemSelected(item);
363         }
364
365         private void dummy() {
366                 NodeDaoImpl dao = (NodeDaoImpl) NodeCacheDao.getInstance();
367                 List<Node> list = dao.search();
368                 for (Node node : list) {
369                         System.out.println(node.toString());
370                 }
371         }
372
373         private void dummy2() {
374                 LockDao dao = (LockDao) LockDao.getInstance();
375                 boolean b;
376
377                 try {
378                         b = dao.lock(this, "key");
379                         b = dao.lock(this, "key");
380                         b = dao.lock(new Object(), "key");
381                         dao.unlock(this, "key");
382                         b = dao.lock(new Object(), "key");
383                         b = dao.lock(this, "key");
384                         b = dao.lock(this, "key");
385                         b = dao.lock(this, "key");
386                         dao.unlock(new Object(), "key");
387
388                 } catch (Exception e) {
389                         e.printStackTrace();
390                 }
391
392         }
393
394         private void selectMenuAdd() {
395                 setStatus(ScreenStatus.AddNode);
396         }
397
398         private void selectMenuDelete() {
399                 setStatus(ScreenStatus.DeleteNode);
400         }
401
402         private void selectMenuRename() {
403                 LabelIconView target = layout.getSelectedTarget();
404                 if (target == null) {
405                         return;
406                 }
407
408                 long id = target.getNodeId();
409                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
410                 Node node = dao.searchById(id);
411                 if (node != null) {
412                         String text = node.getName();
413                         msgEditText.setText(text);
414                         setStatus(ScreenStatus.RenameNode);
415                 }
416         }
417
418         private void selectMenuCut() {
419                 LabelIconView target = layout.getSelectedTarget();
420                 if (target == null) {
421                         return;
422                 }
423
424                 layout.setCutTarget(target);
425                 layout.refresh();
426         }
427
428         private void selectMenuPast() {
429                 LabelIconView srcTarget = layout.getCutTarget();
430                 if (srcTarget == null) {
431                         return;
432                 }
433
434                 LabelIconView dstTarget = layout.getSelectedTarget();
435                 if (dstTarget == null) {
436                         return;
437                 }
438
439                 layout.setCutTarget(null);
440
441                 long srcId = srcTarget.getNodeId();
442                 long dstId = dstTarget.getNodeId();
443                 execute(srcId, dstId);
444         }
445
446         private void updateList() {
447                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
448                 top = dao.searchRoot();
449                 if (top == null) {
450                         // データ0件(データ未更新)
451                         return;
452                 }
453                 updateList(top);
454         }
455
456         private void updateList(Node parent) {
457                 viewY = 10;
458
459                 scrollView.setVisibility(View.GONE);
460                 layout.removeAllViews();
461                 updateList(parent, 0);
462                 layout.setMinimumHeight(viewY);
463                 scrollView.setVisibility(View.VISIBLE);
464                 layout.refresh();
465
466                 scrollView.invalidate();
467         }
468
469         private void updateList(Node node, int depth) {
470                 int N = (int) (bmpWidth * 0.6);
471                 int M = (int) (bmpWidth * 1.2);
472                 int x = N * (depth - indent);
473
474                 if (depth - indent >= 0
475                                 && !(depth - indent == 0 && node instanceof FileNode)) {
476                         LabelIconView view = new LabelIconView(this);
477                         node.setView(view);
478
479                         Bitmap bmp1 = null;
480                         int id = node.getStatusIconId();
481                         if (id >= 0) {
482                                 // DirNode
483                                 bmp1 = bitmapFactory.get(node.getStatusIconId());
484                         } else {
485                                 // FileNode
486                                 x += bmpPaddingLeft;
487                         }
488                         Bitmap bmp2 = bitmapFactory.get(node.getIconId());
489                         createIconImageView(view, bmp1, bmp2, node.getName(), x, viewY);
490                         // LabelIconView s = view.getStatusImageView();
491                         // if (s != null) {
492                         // s.setNodeId(node.getId());
493                         // }
494                         view.setNodeId(node.getId());
495                 }
496                 viewY += M;
497
498                 if (node.getStatus() != Node.Status.OPEN) {
499                         return;
500                 }
501                 List<Long> idList = node.getChildren();
502                 if (idList == null) {
503                         return;
504                 }
505
506                 List<Node> list = new ArrayList<Node>();
507                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
508                 for (Long id : idList) {
509                         Node n = dao.searchById(id);
510                         if (n == null) {
511                                 // ERROR
512                                 continue;
513                         }
514                         if (n instanceof DirNode) {
515                                 DirNode d = (DirNode) n;
516                                 if (isNoteBook(d) && !d.isSelected()) {
517                                         continue;
518                                 }
519                         }
520                         list.add(n);
521                 }
522                 Collections.sort(list, nodeComparator);
523                 for (Node n : list) {
524                         updateList(n, depth + 1);
525                 }
526         }
527
528         private void createIconImageView(LabelIconView v, Bitmap bmp1, Bitmap bmp2,
529                         String text, int x, int y) {
530                 v.init(x, y, bmp1, bmp2, text);
531
532                 android.view.WindowManager.LayoutParams params = new WindowManager.LayoutParams();
533                 params.gravity = Gravity.TOP | Gravity.LEFT;
534                 params.height = WindowManager.LayoutParams.WRAP_CONTENT;
535                 params.width = WindowManager.LayoutParams.WRAP_CONTENT;
536                 params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
537                                 | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
538                                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
539                                 | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
540                 params.format = PixelFormat.TRANSLUCENT;
541                 params.windowAnimations = 0;
542                 params.x = x;
543                 params.y = y;
544                 v.setLayoutParams(params);
545
546                 layout.addView(v);
547                 v.layout(x, y, x + v.getWidth(), y + v.getHeight());
548         }
549
550         @Override
551         public void sendLockResult(boolean b) {
552                 lock = b;
553                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
554
555                 pref.put(Constant.PREF_UPDATE_DATA, Constant.OFF);
556                 pref.update();
557
558                 if (isResult) {
559                         if (dao.isEmpty()) {
560                                 finish();
561                                 return;
562                         }
563                 }
564                 isResult = false;
565
566                 if (dao.isEmpty()) {
567                         if (isInit) {
568                                 Intent intent = new Intent(this,
569                                                 (Class<?>) SettingActivity.class);
570                                 startActivityForResult(intent, 0);
571
572                                 isInit = false;
573                         } else {
574                                 finish();
575                                 isInit = true;
576                         }
577                 }
578
579                 if (isInCycle) {
580                         if (isInit) {
581                                 status = ScreenStatus.Normal;
582
583                                 if (lock) {
584                                         layout.setVisibility(View.VISIBLE);
585                                         updateList();
586                                 } else {
587                                         layout.removeAllViews();
588                                 }
589                                 setStatus();
590                                 isInit = false;
591                         } else {
592                                 if (lock) {
593                                         layout.setVisibility(View.VISIBLE);
594                                         updateList();
595                                 }
596                                 setStatus();
597                         }
598                 } else {
599                         LockDao lockDao = (LockDao) LockDao.getInstance();
600                         lockDao.unlock(EverFolderActivity.this, Constant.LOCK_UPDATE_NOTE);
601                         lock = false;
602                 }
603         }
604
605         @Override
606         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
607                 isResult = true;
608                 isInit = true;
609         }
610
611         @Override
612         protected void onStart() {
613                 super.onStart();
614         }
615
616         @Override
617         protected void onResume() {
618                 super.onResume();
619                 isInCycle = true;
620
621                 // #29573
622                 boolean isUnlock = pref.getInt(Constant.PREF_DELETE_LOCK)== Constant.ON;
623                 if (isUnlock){
624                         pref.put(Constant.PREF_DELETE_LOCK, Constant.OFF);
625                         pref.update();
626                 }
627                 
628                 lockHandler = new LockHandler();
629                 lockHandler.lock(this, Constant.LOCK_UPDATE_NOTE, true, isUnlock);
630                 lock = false;
631
632                 if (isInit) {
633                         layout.setVisibility(View.INVISIBLE);
634                         setStatus();
635                 } else {
636                         // #29589
637                         //// #29421
638                         //updateList();
639                         layout.setVisibility(View.INVISIBLE);
640                         setStatus();
641                 }
642         }
643
644         @Override
645         protected void onPause() {
646                 // ロック未取得でも解除する(ゴミ掃除)
647                 LockDao lockDao = (LockDao) LockDao.getInstance();
648                 lockDao.unlock(this, Constant.LOCK_UPDATE_NOTE);
649                 lock = false;
650
651                 if (lockHandler != null){
652                         lockHandler.cancel();
653                         lockHandler = null;
654                 }
655                 
656                 super.onPause();
657                 isInCycle = false;
658         }
659
660         private void leftButtonOnClick() {
661                 moveButtonOnClick(-1);
662                 setStatus();
663         }
664
665         private void rightButtonOnClick() {
666                 moveButtonOnClick(1);
667                 setStatus();
668         }
669
670         private void moveButtonOnClick(int dx) {
671                 int mx = 0;
672
673                 if (dx < 0) {
674                         if (indent > 0) {
675                                 mx = -1;
676                         }
677                 } else {
678                         mx = 1;
679                 }
680                 if (mx != 0) {
681                         indent += mx;
682                         removeAllNodeView();
683                         updateList();
684                 }
685         }
686
687         private void msgOkButtonOnClick(View v) {
688                 FolderUtil util = FolderUtil.getInstance();
689                 LabelIconView target = layout.getSelectedTarget();
690                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
691
692                 if (status == ScreenStatus.AddNode) {
693                         long id = target.getNodeId();
694                         Node parent = dao.searchById(id);
695                         if (parent != null) {
696                                 String text = msgEditText.getText().toString();
697                                 DirNode node = new DirNode(text, null);
698                                 util.addDirNode(parent, node);
699                         }
700                         layout.setCutTarget(null, false);
701                 } else if (status == ScreenStatus.DeleteNode) {
702                         long id = target.getNodeId();
703                         Node node = dao.searchById(id);
704                         if (node != null) {
705                                 util.deleteNode(node);
706                                 layout.removeView(target);
707                         }
708                         layout.setCutTarget(null, false);
709                 } else if (status == ScreenStatus.RenameNode) {
710                         long id = target.getNodeId();
711                         Node node = dao.searchById(id);
712                         if (node != null) {
713                                 String text = msgEditText.getText().toString();
714
715                                 dao.updateName(node, text);
716                         }
717                         layout.setCutTarget(null, false);
718                 }
719
720                 closeIME(v);
721
722                 layout.clearTarget();
723                 updateList();
724                 setStatus(ScreenStatus.Normal);
725         }
726
727         private void msgCancelOnClick(View v) {
728                 closeIME(v);
729
730                 setStatus(ScreenStatus.Normal);
731         }
732
733         private void closeIME(View v) {
734                 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
735                 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
736         }
737
738         public void execute(long srcId, long dstId) {
739                 try {
740                         removeAllNodeView();
741
742                         NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
743
744                         if (srcId < 0) {
745                                 throw new EfException(EfError.NOT_FOUND);
746                         }
747                         if (dstId < 0) {
748                                 throw new EfException(EfError.NOT_FOUND);
749                         }
750
751                         Node src = dao.searchById(srcId);
752                         Node dst = null;
753                         if (srcId == dstId) {
754                                 dst = src;
755                         } else {
756                                 dst = dao.searchById(dstId);
757                         }
758
759                         if (dstId == src.getParent()) {
760                                 // 同じ場所に移動
761                                 return;
762                         }
763                         if (!isFileNode(src) && !isMovable(srcId, dstId)) {
764                                 // 再帰的な移動
765                                 return;
766                         }
767
768                         Node sp = dao.searchById(src.getParent());
769                         if (sp == null) {
770                                 throw new EfException(EfError.NOT_FOUND);
771                         }
772                         Node dp = null;
773                         if (src.getParent() == dst.getParent() || dst.getParent() < 0) {
774                                 dp = sp;
775                         } else {
776                                 dp = dao.searchById(dst.getParent());
777                         }
778                         if (dp == null) {
779                                 throw new EfException(EfError.NOT_FOUND);
780                         }
781
782                         SQLiteDatabase db = DatabaseHelper.getInstance()
783                                         .getSQLiteDatabase();
784                         db.beginTransaction();
785                         try {
786                                 dao.removeChildrenIdNT(sp, src.getId());
787                                 if (dst instanceof DirNode) {
788                                         dao.updateParentNT(src, dst.getId());
789                                         dao.addChildrenIdNT(dst, src.getId());
790                                 } else if (dst instanceof FileNode) {
791                                         dao.updateParentNT(src, dp.getId());
792                                         dao.addChildrenIdNT(dp, src.getId());
793                                 }
794
795                                 db.setTransactionSuccessful();
796                         } finally {
797                                 db.endTransaction();
798                         }
799                 } catch (EfException e) {
800                         EfException.msg(R.string.ErrorSystem, e);
801                 } finally {
802                         updateList();
803                 }
804         }
805
806         private boolean isMovable(long srcId, long dstId) {
807                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
808
809                 while (dstId >= 0) {
810                         if (srcId == dstId) {
811                                 return false;
812                         }
813                         Node node = dao.searchById(dstId);
814                         if (node == null) {
815                                 // ERROR
816                                 return false;
817                         }
818                         if (!(node instanceof DirNode)) {
819                                 return true;
820                         }
821                         dstId = node.getParent();
822                 }
823                 return true;
824         }
825
826         public void execute(long id) {
827                 try {
828                         NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
829
830                         Node node = dao.searchById(id);
831                         if (node == null) {
832                                 throw new EfException(EfError.NOT_FOUND);
833                         }
834                         node.toggleStatus();
835                         Status status = node.getStatus();
836
837                         dao.updateStatus(node, status);
838
839                         updateList();
840                 } catch (EfException e) {
841                         EfException.msg(R.string.ErrorSystem, e);
842                 }
843         }
844
845         public void executeView(long id) {
846                 NodeDao dao = (NodeDao) NodeCacheDao.getInstance();
847
848                 Node node = dao.searchById(id);
849                 if (!isFileNode(node)) {
850                         return;
851                 }
852                 String guid = node.getGuid();
853
854                 EvernoteIntentUtil util = EvernoteIntentUtil.getInstance();
855                 util.viewNote(this, guid);
856         }
857
858         public void targetSelectedChanged(boolean status) {
859                 setStatus();
860         }
861
862         public void removeAllNodeView() {
863                 layout.removeAllViews();
864         }
865
866         private void setStatus() {
867                 setStatus(status);
868         }
869
870         private void setStatus(ScreenStatus status) {
871                 try {
872                         if (this.status != status) {
873                                 this.status = status;
874                         }
875
876                         if (!lock) {
877                                 int update = pref.getInt(Constant.PREF_UPDATE_DATA);
878
879                                 if (update == Constant.ON) {
880                                         msgLayout.setVisibility(View.VISIBLE);
881                                         confirmMsg.setText(getString(R.string.mainUpdateMsg));
882                                 } else {
883                                         msgLayout.setVisibility(View.GONE);
884                                 }
885                                 msgEditText.setVisibility(View.GONE);
886                                 msgButtonLayout.setVisibility(View.GONE);
887
888                                 leftButton.setEnabled(false);
889                                 rightButton.setEnabled(false);
890
891                                 status = ScreenStatus.Normal;
892                                 return;
893                         }
894
895                         if (status == ScreenStatus.Normal) {
896                                 msgLayout.setVisibility(View.GONE);
897
898                                 boolean leftButtonFlag = true;
899                                 boolean rightButtonFlag = true;
900
901                                 if (indent <= 0) {
902                                         leftButtonFlag = false;
903                                 }
904
905                                 layout.setElabledTouchEvent(true);
906
907                                 leftButton.setEnabled(leftButtonFlag);
908                                 rightButton.setEnabled(rightButtonFlag);
909                         } else if (status == ScreenStatus.AddNode) {
910                                 msgLayout.setVisibility(View.VISIBLE);
911                                 msgEditText.setVisibility(View.VISIBLE);
912                                 msgButtonLayout.setVisibility(View.VISIBLE);
913                                 confirmMsg.setText(getString(R.string.mainAddMsg));
914                                 msgEditText.setText("");
915
916                                 layout.setElabledTouchEvent(false);
917
918                                 leftButton.setEnabled(false);
919                                 rightButton.setEnabled(false);
920                         } else if (status == ScreenStatus.DeleteNode) {
921                                 msgLayout.setVisibility(View.VISIBLE);
922                                 msgEditText.setVisibility(View.GONE);
923                                 msgButtonLayout.setVisibility(View.VISIBLE);
924                                 confirmMsg.setText(getString(R.string.mainDeleteMsg));
925
926                                 layout.setElabledTouchEvent(false);
927
928                                 leftButton.setEnabled(false);
929                                 rightButton.setEnabled(false);
930                         } else if (status == ScreenStatus.RenameNode) {
931                                 msgLayout.setVisibility(View.VISIBLE);
932                                 msgEditText.setVisibility(View.VISIBLE);
933                                 msgButtonLayout.setVisibility(View.VISIBLE);
934                                 confirmMsg.setText(getString(R.string.mainRenameMsg));
935
936                                 layout.setElabledTouchEvent(false);
937
938                                 leftButton.setEnabled(false);
939                                 rightButton.setEnabled(false);
940                         }
941                 } catch (Exception e) {
942                         EfException.msg(R.string.ErrorSystem, e);
943                 }
944         }
945
946 }