Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.buho29.ideamanager.model;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.DialogInterface.OnClickListener;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.text.Spanned;
- import android.text.TextUtils;
- import android.util.Log;
- import android.widget.ImageView;
- import android.widget.Toast;
- import com.buho29.app.FileBrowserActivity;
- import com.buho29.app.ImageViewActivity;
- import com.buho29.data.EnQueue;
- import com.buho29.ideamanager.Main;
- import com.buho29.ideamanager.R;
- import com.buho29.ideamanager.addon.AddonManager;
- import com.buho29.ideamanager.app.IdeaEditActivity;
- import com.buho29.ideamanager.app.NoteEditActivity;
- import com.buho29.ideamanager.app.Preferences;
- import com.buho29.ideamanager.app.TaskActivity;
- import com.buho29.ideamanager.app.ViewSourceActivity;
- import com.buho29.ideamanager.controler.FileManager;
- import com.buho29.ideamanager.data.Task;
- import com.buho29.ideamanager.model.Item.*;
- import com.buho29.ideamanager.model.Model.ErrorEvent;
- import com.buho29.ideamanager.model.Model.Event;
- import com.buho29.ideamanager.model.Model.OnChangedListener;
- import com.buho29.ideamanager.text.Html;
- import com.buho29.utils.Builder;
- import com.buho29.utils.CacheIcon;
- import com.buho29.utils.FileUtils;
- import com.buho29.utils.Launcher;
- import com.buho29.utils.CacheIcon.onIconLoadedListener;
- import com.buho29.utils.FileUtils.OnFileLoadedListener;
- /**
- * @author pp
- *
- */
- public class IdeaManager extends Model{
- private static /*IIdeaModel*/XmlModel model = new XmlModel();
- private static boolean isEnabledEvents = true;
- public static final String CMD= "cmd";
- public static final String PATH= "path";
- public static final String NAME = "name";
- public static final int CMD_NEW= 1;
- public static final int CMD_EDIT=2;
- public static final int CMD_DELETE=3;
- public static final int EVENT_NEW = 1;
- public static final int EVENT_ADDED = 2;
- public static final int EVENT_DELETED = 3;
- public static final int EVENT_RENAMED = 4;
- public static final int EVENT_UPDATE = 5;
- public static final int EVENT_SAVED = 6;
- public static final int EVENT_PASTED = 7;
- public static final int EVENT_ADDED_ITEMS = 8;
- public static final int EVENT_DEMO = 9;
- private static final int EVENT_HIDED = 10;
- public static final int IDEA=1;
- public static final int TASK=2;
- public static ArrayList<Idea> getIdeas() {
- return model.getIdeas();
- }
- public static synchronized void dispach(Event event){
- if(isEnabledEvents)
- Model.dispatch(event);
- }
- public static void loadDemo(Context c){
- enableEvents(false);
- Idea idea = new Idea("Demo",0,"Description proyect");
- add(c,idea);
- String path = idea.getPath();
- Tasks tasks = new Tasks("SampleTask.tasks", path);
- tasks.add(new Task(5, "this is one task", true));
- tasks.add( new Task(2, "cosas por hacer!! ", true));
- tasks.add(new Task(2, "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ", true));
- tasks.save();
- add(c, idea, tasks);
- addFileDemo(c, "lionesas.jpg", idea);
- add(c, idea, new Image("lionesas.jpg", path));
- addFileDemo(c, "main.java", idea);
- add(c, idea,new FileItem("main.java", path));
- addFileDemo(c, "Voix0001.amr", idea);
- add(c, idea,new FileItem("Voix0001.amr", path));
- // recipe
- addFileDemo(c, "recipe.note", idea);
- Note note = new Note("recipe.note", path);
- note.addFile("lionesas.jpg");
- add(c, idea,note);
- addFileDemo(c, "test 1.note", idea);
- note = new Note("test 1.note", path);
- note.addFile("test.note");
- add(c, idea,note);
- addFileDemo(c, "test.note", idea);
- note = new Note("test.note", path);
- note.addFile("Voix0001.amr");
- note.addFile("test 1.note");
- addFileDemo(c, "images.jpeg", idea);
- note.addFile("images.jpeg");
- add(c, idea,note);
- addFileDemo(c, "test.note.0.png", idea);
- new File(idea.getPath(),".thumb").mkdir();
- InputStream in;
- try {
- in = c.getAssets().open("VIDEO0001.3gp");
- FileManager.addFile(".thumb/VIDEO0001.3gp", in, idea);
- } catch (IOException e) {
- e.printStackTrace();
- }
- enableEvents(true);
- dispach(new IdeaEvent(idea, EVENT_DEMO));
- }
- public static void enableEvents(boolean b) {
- isEnabledEvents = b;
- }
- private static boolean addFileDemo(Context context, String name, Idea idea){
- try {
- InputStream in = context.getAssets().open(name);
- FileManager.addFile(name, in, idea);
- return true;
- } catch (Exception e) {
- return false;
- }
- }
- public static void deleteAll() {
- FileManager.deleteAll();
- clear();
- }
- public static void clear() {
- model.clear();
- removeAllListener();
- model = new XmlModel();
- }
- public static Idea getIdea(String path) {
- return model.getIdea(path);
- }
- public static boolean restoreBackup() {
- return model.restoreBackup();
- }
- ///////////////////////////// errors //////////////////////////////////
- private static String getHelpError(Context context,Idea idea){
- String errors = "";
- if(TextUtils.isEmpty(idea.getName()))
- errors+="* Escriba un nombre\n";
- ArrayList<Idea> ideas = getIdeas();
- for (Idea idea1 : ideas) {
- if(idea1.getName().equals(idea.getName())){
- if(context!=null){
- return "* "+context.getString(R.string.exist_idea)+"\n";
- }else return "* el nombre ya existe\n";
- }
- }
- return errors;
- }
- private static String getHelpError(Context c,Idea idea, Item item){
- String errors = "";
- if(isEmptyName(item))
- errors+="* "+c.getString(R.string.empty)+"\n";
- if(idea.contain(item)){
- errors+= "* "+String.format(c.getString(R.string.file_exist), item.getName())+"\n";
- }
- return errors += c.getString(R.string.no_saved);
- }
- private static boolean isEmptyName(Item item) {
- if(item instanceof IContainer){
- IContainer cont = (IContainer) item ;
- return item.getName().length()<= cont.getExtension().length()+1;
- }else return TextUtils.isEmpty(item.getName());
- }
- public static void showError(Context c, String error) {
- Toast.makeText(c,error,Toast.LENGTH_LONG).show();
- }
- public static void showError(Context c, Idea idea, Item item) {
- String errors = getHelpError(c, idea, item);
- showError(c,errors);
- }
- public static void showError(Context c, Idea idea) {
- String errors = getHelpError(c, idea);
- showError(c,errors);
- }
- public static void showSuccess(Context c, String msg) {
- Toast.makeText(c,msg,Toast.LENGTH_LONG).show();
- }
- public static boolean isValide(Idea idea) {
- if(TextUtils.isEmpty(idea.getName())){
- return false;
- }
- ArrayList<Idea> ideas = getIdeas();
- for (Idea idea2 : ideas) {
- if(idea2.getName().equals(idea.getName())){
- return false;
- }
- }
- return true;
- }
- public static boolean isValide(Idea idea, Item item) {
- boolean empty = isEmptyName(item);
- boolean occuped = idea.contain(item);
- return !(empty || occuped);
- }
- ///////////////////////// Idea ///////////////////////////////
- public static boolean add(Context c,Idea idea) {
- if(isValide(idea)){
- boolean result = FileManager.createDirectory(idea);
- if(!result){
- showError(c,"no se pudo crear el directorio de: "+idea.getName());
- return false;
- }else if(model.addIdea(idea)){
- dispach(new IdeaEvent(idea, EVENT_NEW));
- return true;
- }else throw new RuntimeException("no se pudo guardar :"+idea.getName());
- }else {
- showError(c,idea);
- return false;
- }
- }
- public static boolean update(Context c,Idea idea) {
- String name = idea.getName();
- String oldname = idea.getOldName();
- if(!name.equals(oldname) ){
- if(!FileUtils.renameFile(Idea.getFile(oldname), name)){
- showError(c,"no se pudo renombrar de "+oldname+" a "+name);
- idea.setName(oldname);
- return false;
- }else{
- // evitamos doble llamada (xmlmodel)
- //dispach(new IdeaEvent(idea, EVENT_RENAMED));
- }
- }
- boolean result = model.updateIdea(idea);
- if(!result){
- showError(c,"no se pudo actualizar idea");
- return result;
- }else {
- dispach(new IdeaEvent(idea, EVENT_UPDATE));
- return true;
- }
- }
- public static boolean delete(Context c, Idea idea) {
- if(!FileManager.delete(idea)) return false;
- boolean result = model.deleteIdea(idea);
- if(!result){
- showError(c,"no se pudo borrar idea");
- return result;
- }else{
- dispach(new IdeaEvent(idea, EVENT_DELETED));
- return true;
- }
- }
- ////////////////////////// ITEM ///////////////////////////////
- public static boolean add(Context c,Idea idea, Item item) {
- if(!idea.contain(item))
- idea.add(item);
- return true;
- }
- public static boolean delete(Context c,Item item, Idea idea) {
- boolean result = idea.remove(item);
- if(!result){
- showError(c,"no se pudo borrar item en model");
- return false;
- }
- result = true;
- ArrayList<String> files = item.getFiles();
- ArrayList<String> notUseds = getUseds(idea,files);
- for (String file : notUseds) {
- File f = new File(file);
- if(f.exists()){
- if(!f.delete())result = false;
- }
- }
- result = item.delete() && result;
- if(!result){
- showError(c,"no se pudo borrar item");
- return false;
- }else {
- dispach(new ItemEvent(item, EVENT_DELETED));
- return true;
- }
- }
- public static void hideItem(Context c, Item item, Idea idea) {
- boolean result = idea.remove(item);
- if(!result){
- showError(c,"no se pudo borrar item en model");
- }else {
- dispach(new ItemEvent(item, EVENT_HIDED));
- }
- }
- public static boolean update(Context c,Idea idea, Item item) {
- dispach(new ItemEvent(item, EVENT_UPDATE));
- return true;
- }
- public static boolean renameItem(Context c, Item item,String name) {
- boolean success = FileUtils.renameFile(
- item.getFile(), name
- );
- if(success) {
- item.setName(name);
- dispach(new ItemEvent(item, EVENT_RENAMED));
- return true;
- }else {
- showError(c, "no se pudo renombrar item");
- return false;
- }
- }
- /**
- * usado para "pastear" un item
- * @param c
- * @param item
- * @param idea
- * @param listenerOnloaded
- */
- public static void copyItem(Context c, Item item, Idea idea) {
- FileManager.copyItem(c, item, idea);
- }
- /**
- * llamado por Main de fileBrowser y galeria
- * antes lo hacia copyfile!!
- * @param context
- * @param file
- * @param Idea destino
- * @param listenerOnloaded
- */
- public static void addFile(Context c, String file, Idea idea) {
- FileManager.addFile(c, file, idea);
- }
- private static boolean mIsSaved = false;
- ///////////////////////////// UI ////////////////////////////////
- public static Drawable getDrawable(Context c,Idea idea, String source) {
- String path = idea.getPath()+"/"+source;
- //return new BitmapDrawable(BitmapFactory.decodeFile(path));
- try {
- return BitmapDrawable.createFromPath(path);
- } catch (OutOfMemoryError e){
- Toast.makeText(c,e.toString() , Toast.LENGTH_LONG).show();
- Log.e("PaintActivity", "Open image"+e);
- }
- return null;
- }
- public static void loadBitmapIcon(final ImageView iv, Item item) {
- String filePath = item.getSourceIcon();
- if(filePath != null){
- //
- Bitmap b = CacheIcon.get(filePath);
- if(b != null){
- iv.setImageBitmap(b);
- }else{
- iv.setImageBitmap(null);
- CacheIcon.load(filePath, new onIconLoadedListener() {
- @Override
- public void onIconLoaded(Bitmap bitmap) {
- iv.setImageBitmap(bitmap);
- }
- });
- }
- }else{
- int iconId = FileUtils.getIconMimeId(item.getFile());
- iv.setImageResource(iconId);
- }
- }
- ///////////////////////////// Launcher ///////////////////////////////
- public static void openExternal(Activity c, String file) {
- if(FileManager.isXmlFile(file) || file.endsWith(".tasks")){
- launchViewSource(c, file);
- }else Launcher.openExternal(c,file);
- }
- public static void launchFileBrowserActivity(Activity c,Idea idea,int requestCode) {
- String host = Preferences.getHost(c);
- String userName = Preferences.getUser(c);
- String password = Preferences.getPassWord(c);
- Launcher.selectFile(c, requestCode, idea.getPath(),host,userName,password);
- }
- public static void launch(Activity c,Idea idea, String nameFile) {
- Item item = idea.find(nameFile);
- if(item != null) launch(c, idea, item);
- else launch1(c, idea, nameFile);
- }
- private static void launch1(Activity c,Idea idea, String file) {
- if(FileUtils.isNote(file)){
- launchEditNote(c, idea, file);
- }else if(FileUtils.isSource(file)){
- launchViewSource(c,idea.getPath()+"/"+file);
- }else if(FileUtils.isImage(file)){
- launchImageView(c, idea.getPath()+"/"+file);
- }else Launcher.openExternal(c, idea.getPath()+"/"+file);
- }
- public static void launch(Activity c,Idea idea, Item item) {
- String name = item.getName();
- String aPath =item.getAbsolutePath();
- if (item.getFile().exists()) {
- if (item instanceof Note) {
- launchEditNote(c, idea, name);
- } else if (item instanceof Tasks) {
- launchEditTask(c, (Tasks) item);
- } else if (item instanceof Image) {
- launchImageView(c, aPath);
- } else if (item instanceof FileItem && FileUtils.isSource(name)) {
- launchViewSource(c, aPath);
- } else
- Launcher.openExternal(c, aPath);
- }else {
- showError(c, item.getName()+" not found in directory \n the item is erased");
- idea.remove(item);
- dispach(new ItemEvent(item, EVENT_DELETED));
- }
- }
- public static void launchEditNote(Activity c, Idea idea, String name) {
- launchNote(c, idea, name, CMD_EDIT);
- }
- public static void launchNewNote(Activity c, Idea idea, String name) {
- launchNote(c, idea, name, CMD_NEW);
- }
- public static void launchNewTask(Activity c , Idea idea, String name) {
- Intent i = new Intent(c, TaskActivity.class);
- i.putExtra(IdeaManager.CMD, CMD_NEW);
- i.putExtra(IdeaManager.PATH, idea.getName());
- i.putExtra(IdeaManager.NAME, name);
- c.startActivity(i);
- }
- public static void launchEditTask(Activity c ,Tasks tasks) {
- Intent i = new Intent(c, TaskActivity.class);
- i.putExtra(IdeaManager.CMD, CMD_EDIT);
- i.putExtra(IdeaManager.PATH,tasks.getDirectory() );
- i.putExtra(IdeaManager.NAME,tasks.getName() );
- c.startActivity(i);
- }
- private static void launchNote(Activity c,Idea idea,String name,int cmd) {
- Intent i = new Intent(c, NoteEditActivity.class);
- i.putExtra(IdeaManager.CMD, cmd);
- i.putExtra(IdeaManager.PATH, idea.getName());
- i.putExtra(IdeaManager.NAME, name);
- c.startActivityForResult(i, Main.ACTIVITY_NOTE);
- }
- private static void launchImageView(Activity c, String path) {
- Intent i = new Intent(c, ImageViewActivity.class);
- i.putExtra(ImageViewActivity.PATH, path );
- c.startActivity(i);
- }
- private static void launchViewSource(Activity c,String path) {
- Intent i = new Intent(c, ViewSourceActivity.class);
- i.putExtra(ViewSourceActivity.PATH, path );
- c.startActivity(i);
- }
- public static ArrayList<String> getUseds(Idea idea,ArrayList<String> files){
- HashMap<String,Integer> map = new HashMap<String, Integer>();
- ArrayList<Item>items = idea.getItems();
- for (Item item : items) {
- String path = item.getName();
- if(!map.containsKey(path)){
- map.put(path,1);
- }else{
- int used = map.get(path);
- map.put(path,used+1);
- }
- }
- ArrayList<String> notUseds = new ArrayList<String>();
- for (String file : files) {
- if(!map.containsKey(file)){
- notUseds.add(idea.getPath()+"/"+file);
- }
- }
- return notUseds;
- }
- /**
- *
- * @return devuelve true cuando se guardo model
- * y se pone a false para la proxima lectura
- * note: usado en uni test
- */
- public static boolean isSaved() {
- return model.isSaved();
- }
- //////////////////////////// EVENTS /////////////////////////
- public static class IdeaEvent extends Event{
- private Idea mIdea;
- public IdeaEvent(Idea idea,int type) {
- super(type);
- mIdea = idea;
- }
- @Override
- public Object getData() {
- return mIdea;
- }
- }
- public static class ImportEvent extends Event{
- public ImportEvent() {
- super(0);
- }
- @Override
- public Object getData() {
- return null;
- }
- }
- public static class ItemEvent extends Event{
- private Item mItem;
- public ItemEvent(Item item,int type) {
- super(type);
- mItem = item;
- }
- @Override
- public Object getData() {
- return mItem;
- }
- }
- /**
- * llamado para mostrar errores asyncronas (pillar context)
- * @param c
- * @param event
- */
- public static void showError(Context c, ErrorEvent event) {
- // en principio cuando peta guardar o copiando en lote
- /*showError(c, "Error event : "+
- event.getData().getClass().getSimpleName()+
- "/n exception :"+event.getError().getMessage());*/
- Builder.showException(c, event.getError());
- }
- public static void showError(Context c, int resId) {
- showSuccess(c, c.getString(resId));
- }
- public static boolean existsV1() {
- return false;//new File("/mnt/sdcard/ideamanager/ideas.xml").exists();
- }
- public static boolean importV1(Context c) {
- return model.importV1(c,"/mnt/sdcard/ideamanager");
- }
- public static void deleteV1(Context c) {
- if(FileUtils.deleteDire(new File("/mnt/sdcard/ideamanager")) >0){
- showError(c, "no se pudo pudo borrar");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement