Advertisement
Guest User

Untitled

a guest
Nov 6th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.50 KB | None | 0 0
  1. package com.buho29.ideamanager.model;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8.  
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.content.Intent;
  12. import android.content.DialogInterface.OnClickListener;
  13. import android.graphics.Bitmap;
  14. import android.graphics.drawable.BitmapDrawable;
  15. import android.graphics.drawable.Drawable;
  16. import android.text.Spanned;
  17. import android.text.TextUtils;
  18. import android.util.Log;
  19. import android.widget.ImageView;
  20. import android.widget.Toast;
  21.  
  22. import com.buho29.app.FileBrowserActivity;
  23. import com.buho29.app.ImageViewActivity;
  24. import com.buho29.data.EnQueue;
  25. import com.buho29.ideamanager.Main;
  26. import com.buho29.ideamanager.R;
  27. import com.buho29.ideamanager.addon.AddonManager;
  28. import com.buho29.ideamanager.app.IdeaEditActivity;
  29. import com.buho29.ideamanager.app.NoteEditActivity;
  30. import com.buho29.ideamanager.app.Preferences;
  31. import com.buho29.ideamanager.app.TaskActivity;
  32. import com.buho29.ideamanager.app.ViewSourceActivity;
  33. import com.buho29.ideamanager.controler.FileManager;
  34. import com.buho29.ideamanager.data.Task;
  35. import com.buho29.ideamanager.model.Item.*;
  36. import com.buho29.ideamanager.model.Model.ErrorEvent;
  37. import com.buho29.ideamanager.model.Model.Event;
  38. import com.buho29.ideamanager.model.Model.OnChangedListener;
  39. import com.buho29.ideamanager.text.Html;
  40. import com.buho29.utils.Builder;
  41. import com.buho29.utils.CacheIcon;
  42. import com.buho29.utils.FileUtils;
  43. import com.buho29.utils.Launcher;
  44. import com.buho29.utils.CacheIcon.onIconLoadedListener;
  45. import com.buho29.utils.FileUtils.OnFileLoadedListener;
  46.  
  47.  
  48. /**
  49.  * @author pp
  50.  *
  51.  */
  52. public class IdeaManager extends Model{
  53.    
  54.     private static /*IIdeaModel*/XmlModel model = new XmlModel();
  55.  
  56.     private static boolean isEnabledEvents = true;
  57.    
  58.     public static final String CMD= "cmd";
  59.     public static final String PATH= "path";
  60.     public static final String NAME = "name";
  61.     public static final int CMD_NEW= 1;
  62.     public static final int CMD_EDIT=2;
  63.     public static final int CMD_DELETE=3;
  64.  
  65.     public static final int EVENT_NEW           = 1;
  66.     public static final int EVENT_ADDED         = 2;
  67.     public static final int EVENT_DELETED       = 3;
  68.     public static final int EVENT_RENAMED       = 4;
  69.     public static final int EVENT_UPDATE        = 5;
  70.     public static final int EVENT_SAVED         = 6;
  71.     public static final int EVENT_PASTED        = 7;
  72.     public static final int EVENT_ADDED_ITEMS   = 8;
  73.     public static final int EVENT_DEMO          = 9;
  74.     private static final int EVENT_HIDED        = 10;
  75.    
  76.     public static final int IDEA=1;
  77.     public static final int TASK=2;
  78.  
  79.  
  80.  
  81.  
  82.     public static ArrayList<Idea> getIdeas() {
  83.         return model.getIdeas();
  84.     }
  85.    
  86.     public static synchronized void dispach(Event event){
  87.         if(isEnabledEvents)
  88.             Model.dispatch(event);
  89.     }
  90.    
  91.    
  92.     public static void loadDemo(Context c){
  93.        
  94.         enableEvents(false);
  95.        
  96.         Idea idea = new Idea("Demo",0,"Description proyect");
  97.         add(c,idea);
  98.         String path = idea.getPath();
  99.         Tasks tasks = new Tasks("SampleTask.tasks", path);
  100.        
  101.         tasks.add(new Task(5, "this is one task", true));
  102.         tasks.add( new Task(2, "cosas por hacer!! ", true));
  103.         tasks.add(new Task(2, "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ", true));
  104.         tasks.save();
  105.        
  106.         add(c, idea, tasks);
  107.        
  108.         addFileDemo(c, "lionesas.jpg", idea);
  109.         add(c, idea, new Image("lionesas.jpg", path));
  110.        
  111.         addFileDemo(c, "main.java", idea);
  112.         add(c, idea,new FileItem("main.java", path));
  113.        
  114.         addFileDemo(c, "Voix0001.amr", idea);
  115.         add(c, idea,new FileItem("Voix0001.amr", path));
  116.        
  117.         // recipe
  118.         addFileDemo(c, "recipe.note", idea);
  119.         Note note = new Note("recipe.note", path);
  120.         note.addFile("lionesas.jpg");
  121.         add(c, idea,note);
  122.        
  123.         addFileDemo(c, "test 1.note", idea);
  124.         note = new Note("test 1.note", path);
  125.         note.addFile("test.note");
  126.         add(c, idea,note);
  127.        
  128.         addFileDemo(c, "test.note", idea);
  129.         note = new Note("test.note", path);
  130.         note.addFile("Voix0001.amr");
  131.         note.addFile("test 1.note");
  132.        
  133.         addFileDemo(c, "images.jpeg", idea);
  134.         note.addFile("images.jpeg");
  135.        
  136.         add(c, idea,note);
  137.        
  138.         addFileDemo(c, "test.note.0.png", idea);
  139.        
  140.         new File(idea.getPath(),".thumb").mkdir();
  141.         InputStream in;
  142.         try {
  143.             in = c.getAssets().open("VIDEO0001.3gp");
  144.             FileManager.addFile(".thumb/VIDEO0001.3gp", in, idea);
  145.         } catch (IOException e) {
  146.             e.printStackTrace();
  147.         }
  148.  
  149.         enableEvents(true);
  150.         dispach(new IdeaEvent(idea, EVENT_DEMO));
  151.     }  
  152.     public static void enableEvents(boolean b) {
  153.         isEnabledEvents = b;
  154.     }
  155.    
  156.     private static boolean addFileDemo(Context context, String name, Idea idea){
  157.         try {
  158.             InputStream in = context.getAssets().open(name);
  159.             FileManager.addFile(name, in, idea);
  160.             return true;
  161.         } catch (Exception e) {
  162.             return false;
  163.         }
  164.     }
  165.        
  166.     public static void deleteAll() {
  167.         FileManager.deleteAll();
  168.         clear();
  169.     }
  170.    
  171.     public static void clear() {
  172.         model.clear();
  173.         removeAllListener();
  174.         model = new XmlModel();
  175.     }
  176.     public static Idea getIdea(String path) {
  177.         return model.getIdea(path);
  178.     }
  179.     public static boolean restoreBackup() {
  180.         return model.restoreBackup();
  181.     }
  182.    
  183.    
  184.     ///////////////////////////// errors //////////////////////////////////
  185.    
  186.     private static String getHelpError(Context context,Idea idea){
  187.         String errors = "";
  188.         if(TextUtils.isEmpty(idea.getName()))
  189.             errors+="* Escriba un nombre\n";
  190.         ArrayList<Idea> ideas = getIdeas();
  191.         for (Idea idea1 : ideas) {
  192.             if(idea1.getName().equals(idea.getName())){
  193.                 if(context!=null){
  194.                     return "* "+context.getString(R.string.exist_idea)+"\n";
  195.                 }else return "* el nombre ya existe\n";
  196.             }
  197.         }
  198.         return errors;
  199.     }
  200.    
  201.     private static String getHelpError(Context c,Idea idea, Item item){
  202.         String errors = "";
  203.        
  204.         if(isEmptyName(item))
  205.             errors+="* "+c.getString(R.string.empty)+"\n";
  206.         if(idea.contain(item)){
  207.              errors+= "* "+String.format(c.getString(R.string.file_exist), item.getName())+"\n";
  208.         }
  209.         return errors += c.getString(R.string.no_saved);
  210.     }
  211.  
  212.     private static boolean isEmptyName(Item item) {
  213.         if(item instanceof IContainer){
  214.             IContainer cont = (IContainer) item ;
  215.             return item.getName().length()<= cont.getExtension().length()+1;
  216.         }else return TextUtils.isEmpty(item.getName());
  217.     }
  218.        
  219.     public static void showError(Context c, String error) {
  220.         Toast.makeText(c,error,Toast.LENGTH_LONG).show();
  221.     }
  222.    
  223.     public static void showError(Context c, Idea idea, Item item) {
  224.         String errors = getHelpError(c, idea, item);
  225.         showError(c,errors);
  226.     }
  227.    
  228.     public static void showError(Context c, Idea idea) {
  229.         String errors = getHelpError(c, idea);
  230.         showError(c,errors);
  231.     }
  232.  
  233.     public static void showSuccess(Context c, String msg) {
  234.         Toast.makeText(c,msg,Toast.LENGTH_LONG).show();
  235.     }
  236.    
  237.     public static boolean isValide(Idea idea) {
  238.         if(TextUtils.isEmpty(idea.getName())){
  239.             return false;
  240.         }
  241.         ArrayList<Idea> ideas = getIdeas();
  242.         for (Idea idea2 : ideas) {
  243.             if(idea2.getName().equals(idea.getName())){
  244.                 return false;
  245.             }
  246.         }
  247.         return true;
  248.     }
  249.    
  250.     public static boolean isValide(Idea idea, Item item) {
  251.         boolean empty = isEmptyName(item);
  252.         boolean occuped = idea.contain(item);
  253.         return !(empty || occuped);
  254.     }
  255.    
  256.     ///////////////////////// Idea ///////////////////////////////
  257.    
  258.     public static boolean add(Context c,Idea idea) {
  259.         if(isValide(idea)){
  260.             boolean result = FileManager.createDirectory(idea);
  261.             if(!result){
  262.                 showError(c,"no se pudo crear el directorio de: "+idea.getName());
  263.                 return false;
  264.             }else if(model.addIdea(idea)){
  265.                 dispach(new IdeaEvent(idea, EVENT_NEW));
  266.                 return true;
  267.             }else throw new RuntimeException("no se pudo guardar :"+idea.getName());
  268.         }else {
  269.             showError(c,idea);
  270.             return false;
  271.         }
  272.     }
  273.     public static boolean update(Context c,Idea idea) {
  274.         String name = idea.getName();
  275.         String oldname = idea.getOldName();
  276.         if(!name.equals(oldname) ){
  277.             if(!FileUtils.renameFile(Idea.getFile(oldname), name)){
  278.                 showError(c,"no se pudo renombrar de "+oldname+" a "+name);
  279.                 idea.setName(oldname);
  280.                 return false;
  281.             }else{
  282.                 // evitamos doble llamada (xmlmodel)
  283.                 //dispach(new IdeaEvent(idea, EVENT_RENAMED)); 
  284.             }
  285.         }
  286.         boolean result = model.updateIdea(idea);
  287.         if(!result){
  288.             showError(c,"no se pudo actualizar idea");
  289.             return result;
  290.         }else {
  291.             dispach(new IdeaEvent(idea, EVENT_UPDATE));
  292.             return true;
  293.         }
  294.     }
  295.    
  296.     public static boolean delete(Context c, Idea idea) {
  297.         if(!FileManager.delete(idea)) return false;
  298.         boolean result = model.deleteIdea(idea);
  299.         if(!result){
  300.             showError(c,"no se pudo borrar idea");
  301.             return result;
  302.         }else{
  303.             dispach(new IdeaEvent(idea, EVENT_DELETED));
  304.             return true;
  305.         }
  306.     }
  307.    
  308.     ////////////////////////// ITEM ///////////////////////////////
  309.    
  310.     public static boolean add(Context c,Idea idea, Item item) {
  311.         if(!idea.contain(item))
  312.             idea.add(item);
  313.         return true;
  314.     }  
  315.     public static boolean delete(Context c,Item item, Idea idea) {
  316.         boolean result = idea.remove(item);
  317.         if(!result){
  318.             showError(c,"no se pudo borrar item en model");
  319.             return false;
  320.         }
  321.         result = true;
  322.         ArrayList<String> files = item.getFiles();
  323.         ArrayList<String> notUseds = getUseds(idea,files);
  324.         for (String file : notUseds) {
  325.             File f = new File(file);
  326.             if(f.exists()){
  327.                 if(!f.delete())result = false;
  328.             }
  329.         }
  330.         result = item.delete() && result;
  331.         if(!result){
  332.             showError(c,"no se pudo borrar item");
  333.             return false;
  334.         }else {
  335.             dispach(new ItemEvent(item, EVENT_DELETED));
  336.             return true;
  337.         }
  338.     }
  339.  
  340.     public static void hideItem(Context c, Item item, Idea idea) {
  341.         boolean result = idea.remove(item);
  342.         if(!result){
  343.             showError(c,"no se pudo borrar item en model");
  344.         }else {
  345.             dispach(new ItemEvent(item, EVENT_HIDED));
  346.         }
  347.     }
  348.    
  349.     public static boolean update(Context c,Idea idea, Item item) {
  350.         dispach(new ItemEvent(item, EVENT_UPDATE));
  351.         return true;
  352.     }
  353.  
  354.     public static boolean renameItem(Context c, Item item,String name) {
  355.        
  356.         boolean success = FileUtils.renameFile(
  357.             item.getFile(), name
  358.         );
  359.        
  360.         if(success) {
  361.             item.setName(name);
  362.             dispach(new ItemEvent(item, EVENT_RENAMED));
  363.             return true;
  364.         }else {
  365.             showError(c, "no se pudo renombrar item");
  366.             return false;
  367.         }
  368.     }
  369.  
  370.     /**
  371.      * usado para "pastear" un item
  372.      * @param c
  373.      * @param item
  374.      * @param idea
  375.      * @param listenerOnloaded
  376.      */
  377.     public static void copyItem(Context c, Item item, Idea idea) {
  378.         FileManager.copyItem(c, item, idea);
  379.     }
  380.  
  381.     /**
  382.      *  llamado por Main de fileBrowser y galeria
  383.      *  antes lo hacia copyfile!!
  384.      * @param context
  385.      * @param file
  386.      * @param Idea destino
  387.      * @param listenerOnloaded
  388.      */
  389.     public static void addFile(Context c, String file, Idea idea) {
  390.         FileManager.addFile(c, file, idea);
  391.     }
  392.    
  393.     private static boolean mIsSaved = false;
  394.  
  395.        
  396.     ///////////////////////////// UI ////////////////////////////////
  397.    
  398.     public static Drawable getDrawable(Context c,Idea idea, String source) {
  399.         String path = idea.getPath()+"/"+source;
  400.        
  401.         //return new BitmapDrawable(BitmapFactory.decodeFile(path));
  402.        
  403.         try {
  404.             return  BitmapDrawable.createFromPath(path);
  405.         } catch (OutOfMemoryError e){
  406.             Toast.makeText(c,e.toString() , Toast.LENGTH_LONG).show();
  407.             Log.e("PaintActivity", "Open image"+e);
  408.         }
  409.         return null;
  410.     }
  411.    
  412.     public static void loadBitmapIcon(final ImageView iv, Item item) {
  413.        
  414.         String filePath = item.getSourceIcon();
  415.        
  416.         if(filePath != null){
  417.             //
  418.             Bitmap b = CacheIcon.get(filePath);
  419.             if(b != null){
  420.                 iv.setImageBitmap(b);
  421.             }else{
  422.                 iv.setImageBitmap(null);
  423.                 CacheIcon.load(filePath, new onIconLoadedListener() {
  424.                     @Override
  425.                     public void onIconLoaded(Bitmap bitmap) {
  426.                         iv.setImageBitmap(bitmap);
  427.                     }
  428.                 });
  429.             }
  430.         }else{
  431.             int iconId = FileUtils.getIconMimeId(item.getFile());  
  432.             iv.setImageResource(iconId);
  433.         }
  434.     }
  435.  
  436.     ///////////////////////////// Launcher ///////////////////////////////
  437.    
  438.     public static void openExternal(Activity c, String file) {
  439.         if(FileManager.isXmlFile(file) || file.endsWith(".tasks")){
  440.             launchViewSource(c, file);
  441.         }else Launcher.openExternal(c,file);
  442.     }
  443.    
  444.     public static void launchFileBrowserActivity(Activity c,Idea idea,int requestCode) {
  445.         String host = Preferences.getHost(c);
  446.         String userName = Preferences.getUser(c);
  447.         String password = Preferences.getPassWord(c);
  448.         Launcher.selectFile(c, requestCode, idea.getPath(),host,userName,password);
  449.     }
  450.    
  451.     public static void launch(Activity c,Idea idea, String nameFile) {
  452.         Item item = idea.find(nameFile);
  453.         if(item != null) launch(c, idea, item);
  454.         else launch1(c, idea, nameFile);
  455.     }
  456.    
  457.     private static void launch1(Activity c,Idea idea, String file) {
  458.         if(FileUtils.isNote(file)){
  459.             launchEditNote(c, idea, file);
  460.         }else if(FileUtils.isSource(file)){
  461.             launchViewSource(c,idea.getPath()+"/"+file);
  462.         }else if(FileUtils.isImage(file)){
  463.             launchImageView(c, idea.getPath()+"/"+file);
  464.         }else Launcher.openExternal(c, idea.getPath()+"/"+file);
  465.     }
  466.    
  467.     public static void launch(Activity c,Idea idea, Item item) {
  468.         String name = item.getName();
  469.         String aPath =item.getAbsolutePath();
  470.         if (item.getFile().exists()) {
  471.             if (item instanceof Note) {
  472.                 launchEditNote(c, idea, name);
  473.             } else if (item instanceof Tasks) {
  474.                 launchEditTask(c, (Tasks) item);
  475.             } else if (item instanceof Image) {
  476.                 launchImageView(c, aPath);
  477.             } else if (item instanceof FileItem && FileUtils.isSource(name)) {
  478.                 launchViewSource(c, aPath);
  479.             } else
  480.                 Launcher.openExternal(c, aPath);
  481.         }else {
  482.             showError(c, item.getName()+" not found in directory \n the item is erased");
  483.             idea.remove(item);
  484.             dispach(new ItemEvent(item, EVENT_DELETED));
  485.         }
  486.     }
  487.    
  488.     public static void launchEditNote(Activity c, Idea idea, String name) {
  489.         launchNote(c, idea, name, CMD_EDIT);
  490.     }
  491.    
  492.     public static void launchNewNote(Activity c, Idea idea, String name) {
  493.         launchNote(c, idea, name, CMD_NEW);
  494.     }
  495.  
  496.     public static void launchNewTask(Activity c , Idea idea, String name) {
  497.        
  498.         Intent i = new Intent(c, TaskActivity.class);
  499.         i.putExtra(IdeaManager.CMD, CMD_NEW);
  500.         i.putExtra(IdeaManager.PATH, idea.getName());
  501.         i.putExtra(IdeaManager.NAME, name);
  502.        
  503.         c.startActivity(i);
  504.     }
  505.    
  506.     public static void launchEditTask(Activity c ,Tasks tasks) {
  507.         Intent i = new Intent(c, TaskActivity.class);
  508.         i.putExtra(IdeaManager.CMD, CMD_EDIT);
  509.         i.putExtra(IdeaManager.PATH,tasks.getDirectory() );
  510.         i.putExtra(IdeaManager.NAME,tasks.getName() );
  511.         c.startActivity(i);
  512.     }
  513.    
  514.     private static void launchNote(Activity c,Idea idea,String name,int cmd) {
  515.         Intent i = new Intent(c, NoteEditActivity.class);
  516.         i.putExtra(IdeaManager.CMD, cmd);
  517.         i.putExtra(IdeaManager.PATH, idea.getName());
  518.         i.putExtra(IdeaManager.NAME, name);
  519.         c.startActivityForResult(i, Main.ACTIVITY_NOTE);
  520.     }
  521.  
  522.     private static void launchImageView(Activity c, String path) {
  523.         Intent i = new Intent(c, ImageViewActivity.class);
  524.         i.putExtra(ImageViewActivity.PATH, path );
  525.         c.startActivity(i);
  526.     }
  527.  
  528.     private static void launchViewSource(Activity c,String path) {
  529.         Intent i = new Intent(c, ViewSourceActivity.class);
  530.         i.putExtra(ViewSourceActivity.PATH, path );
  531.         c.startActivity(i);
  532.     }
  533.    
  534.     public static ArrayList<String> getUseds(Idea idea,ArrayList<String> files){
  535.         HashMap<String,Integer> map = new HashMap<String, Integer>();
  536.         ArrayList<Item>items = idea.getItems();
  537.         for (Item item : items) {
  538.             String path = item.getName();
  539.             if(!map.containsKey(path)){
  540.                 map.put(path,1);
  541.             }else{
  542.                 int used = map.get(path);
  543.                 map.put(path,used+1);
  544.             }
  545.         }
  546.        
  547.         ArrayList<String> notUseds = new ArrayList<String>();
  548.         for (String file : files) {
  549.             if(!map.containsKey(file)){
  550.                 notUseds.add(idea.getPath()+"/"+file);
  551.             }
  552.         }
  553.         return notUseds;
  554.     }
  555.  
  556.     /**
  557.      *
  558.      * @return devuelve true cuando se guardo model
  559.      *         y se pone a false para la proxima lectura
  560.      *         note: usado en uni test
  561.      */
  562.     public static boolean isSaved() {
  563.         return model.isSaved();
  564.     }
  565.  
  566.     //////////////////////////// EVENTS /////////////////////////  
  567.    
  568.     public static class IdeaEvent extends Event{
  569.  
  570.         private Idea mIdea;
  571.         public IdeaEvent(Idea idea,int type) {
  572.             super(type);
  573.             mIdea = idea;
  574.         }
  575.         @Override
  576.         public Object getData() {
  577.             return mIdea;
  578.         }
  579.        
  580.     }
  581.        
  582.     public static class ImportEvent extends Event{
  583.  
  584.         public ImportEvent() {
  585.             super(0);
  586.         }
  587.  
  588.         @Override
  589.         public Object getData() {
  590.             return null;
  591.         }
  592.        
  593.     }
  594.     public static class ItemEvent extends Event{
  595.         private Item mItem;
  596.         public ItemEvent(Item item,int type) {
  597.             super(type);
  598.             mItem = item;
  599.         }
  600.         @Override
  601.         public Object getData() {
  602.             return mItem;
  603.         }
  604.        
  605.     }
  606.  
  607.     /**
  608.      * llamado para mostrar errores asyncronas (pillar context)
  609.      * @param c
  610.      * @param event
  611.      */
  612.     public static void showError(Context c, ErrorEvent event) {
  613.         // en principio cuando peta guardar o copiando en lote
  614.         /*showError(c, "Error event : "+
  615.                 event.getData().getClass().getSimpleName()+
  616.                 "/n exception :"+event.getError().getMessage());*/
  617.         Builder.showException(c, event.getError());
  618.     }
  619.  
  620.  
  621.     public static void showError(Context c, int resId) {
  622.         showSuccess(c, c.getString(resId));
  623.     }
  624.  
  625.     public static boolean existsV1() {
  626.         return false;//new File("/mnt/sdcard/ideamanager/ideas.xml").exists();
  627.     }
  628.  
  629.     public static boolean importV1(Context c) {
  630.         return model.importV1(c,"/mnt/sdcard/ideamanager");
  631.     }
  632.  
  633.     public static void deleteV1(Context c) {
  634.         if(FileUtils.deleteDire(new File("/mnt/sdcard/ideamanager")) >0){
  635.             showError(c, "no se pudo pudo borrar");
  636.         }
  637.     }
  638.  
  639.  
  640. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement