DbUpdateUtils.java 5.49 KB
package com.gimi.common.cinema.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.gimi.common.cinema.db.NewDBManager;
import com.gimi.common.cinema.model.AsyncCallback;
import com.gimi.common.cinema.model.Constant;
import com.gimi.common.cinema.model.LocalMovieMessage;
import com.gimi.common.cinema.model.MovieMessage;
import com.gimi.common.cinema.model.Rating;
import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.model.WrongMsg;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库辅助
 * Created by 李攀 on 2015/5/7.
 */
public class DbUpdateUtils {
    private Context context;
    private SambaMsg sambaMsg;
    private NewDBManager dbManager;
    private String localPath;
    private String rootPath;
    private String folder;

    /**
     * init
     * sometimes need charge localPath exists and remount
     *
     * @param context
     */
    public DbUpdateUtils(Context context) {
        this.context = context;
        dbManager = NewDBManager.getInstance();
        SharedPreferences sharedPreferences = context.getSharedPreferences(Constant.XML_NAME, Context.MODE_PRIVATE);
        sambaMsg = Utils.getSambaMsg(sharedPreferences);
        folder = sambaMsg.getFolder();
        localPath = sambaMsg.getLocalPath();
        rootPath = sambaMsg.getRootPath();
    }

    /**
     * @param callback success
     */
    public void addAllLocalData(final AsyncCallback<Integer> callback) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                WrongMsg caught = new WrongMsg();
                callback.onStart();
                try {
                    LocalMovieScanUtils scanUtils = new LocalMovieScanUtils(context);
                    ArrayList<LocalMovieMessage> allLocalMovies;
//                    if (BuildConfig.MACHINE_TYPE.equals("himedia")) {
                        allLocalMovies = scanUtils.getAllLocalMovie(rootPath, folder, callback);
//                    }else{
//                        allLocalMovies = scanUtils.getAllLocalMovie(sambaMsg,callback);
//                    }
                    //区分结束还是卡死
                    callback.onMessage("已扫描:" + allLocalMovies.size());
                    if (allLocalMovies.size() != 0) {
                        dbManager.addByTransaction(allLocalMovies);
                        callback.onSuccess(allLocalMovies.size());
                    } else {
                        caught.setCode(0);
                        callback.onFailure(caught);
                    }
                } catch (Exception e) {
                    caught.setCode(1);
                    callback.onFailure(caught);
                    e.printStackTrace();
                } finally {
                    callback.onDone();
                }
            }
        }.start();
    }

    int i = 0;

    public void deleteMovieNotExists(final AsyncCallback<Integer> callback) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                callback.onStart();
                List<LocalMovieMessage> query = dbManager.query();
                for (LocalMovieMessage localMovieMessage : query) {
                    if (!new File(localMovieMessage.getPlayPath()).exists()) {
                        dbManager.delete(localMovieMessage);
                        i++;
                        Log.d("lovely", "delete success " + localMovieMessage.getMovieName());
                    }
                }
                callback.onSuccess(i);
                i = 0;
                callback.onDone();
            }
        }.start();
    }

    public void initDoubanMsg(final AsyncCallback<Integer> callback) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                callback.onStart();
                //query the class describe or the douban rating is null
                List<LocalMovieMessage> query = dbManager.query();
                for (LocalMovieMessage localMovieMessage : query) {
                    String movieFolderPath = FolderUtils.getMovieFolderPath(localMovieMessage.getPlayPath(), localPath, null);
                    MovieMessage mm;
                    //Read it out and show on the ui
                    mm = MovieMessageUtils.getLocalMovieMessage(movieFolderPath);
                    if (mm == null) {
                        continue;
                    }
                    Rating rating = mm.getRating();
                    double average = 0;//rating
                    if (rating != null) {
                        average = rating.getAverage();
                    }
                    String[] genres = mm.getGenres();
                    //types
                    StringBuilder types = new StringBuilder();
                    for (String s : genres) {
                        types.append(s + "/");
                    } //clazz
                    // how to get the movie length
                    localMovieMessage.setDoubanRating(average);
                    localMovieMessage.setDoubanId(mm.getId());
                    localMovieMessage.setClassDescribe(types.toString());
                    localMovieMessage.setScreenTime(mm.getYear());
                    dbManager.updateDoubanMsg(localMovieMessage);
                }
                callback.onSuccess(i);
                i = 0;
                callback.onDone();
            }
        }.start();
    }
}