OtherModelImpl.java 7.56 KB
/*
 * Copyright (c) 2016 wugian
 * 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.xgimi.gimicinema.model;

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

import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.utils.FileReadUtils;
import com.gimi.common.cinema.utils.SambaFileCharge;
import com.gimi.common.cinema.utils.ScanUtils;
import com.gimi.common.cinema.utils.Sort;
import com.gimi.common.cinema.utils.SortUtils;
import com.gimi.common.cinema.utils.Utils;
import com.google.gson.Gson;
import com.xgimi.gimicinema.BuildConfig;
import com.xgimi.gimicinema.activity.CinemaConfig;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import static android.R.attr.path;

/**
 * Created by wugian on 2016/9/28
 */
public class OtherModelImpl implements IOtherModel {
    private Handler handler = new Handler();

    @Override
    public void getAllApk(String path, OnGetApkList l) {
        ArrayList<HashMap<String, Object>> data = new ArrayList<>();
        File apkFile = new File(path);
        if (SambaFileCharge.fileExist(apkFile.toString())) {
            String[] list = apkFile.list();
            for (String s : list) {
                HashMap<String, Object> tempHashMap = new HashMap<>();
                tempHashMap.put("userName", s);
                data.add(tempHashMap);
            }
        }
        l.onGetApkListSuccess(data);
    }

    @Override
    public void updateClazz(final SharedPreferences sp, final OnUpdateClazzListener l) {
        ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
            @Override
            public void doSomeThing() {
                SambaMsg msg = Utils.getSambaMsg(sp);
                String localPath = msg.getLocalPath();
                com.gimi.common.cinema.model.CinemaConfig cfgLocal = Utils.readConfigBySP(sp);
                if (BuildConfig.MACHINE_TYPE.equals("himedia")) {
                    localPath = CinemaConfig.BASIC_ROOT + "/";
                }

                com.gimi.common.cinema.model.CinemaConfig cfgSmb =
                        FileReadUtils.readConfigBySamba(localPath + ".time");
                if (cfgLocal == null || (cfgLocal.getClassArray() != null &&
                        cfgLocal.getClassArray().length < 1)) {
                    return;
                }

                if (cfgLocal.getClassVersion() < cfgSmb.getClassVersion()) {
                    if (cfgSmb.getClassArray() != null
                            && cfgSmb.getClassArray().length > 0) {
                        StringBuilder stringBuilder = new StringBuilder();
                        for (String trim : cfgSmb.getClassArray()) {
                            if (!trim.isEmpty()) {
                                if (stringBuilder.length() != 0) {
                                    stringBuilder.append("@");
                                }
                                stringBuilder.append(trim);
                            }
                        }
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putString("classifications", stringBuilder.toString());
                        edit.putInt("classifications-version",
                                cfgSmb.getClassVersion());
                        edit.apply();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                l.onUpdateClazzSuccess();
                            }
                        });
                    }
                } else if (cfgLocal.getClassVersion() > cfgSmb.getClassVersion()) {
                    String content = new Gson().toJson(cfgLocal);
                    FileReadUtils.writeDates(msg.getConfigPath(), content, false);
                } else {
                    Log.d("lovely", "╟version is same  ");
                }
            }
        });
    }

    @Override
    public void scanOtherMovies(final Context context, final String path,
                                final OnScanOtherMovieListener l) {
        ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
            @Override
            public void doSomeThing() {

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        l.onScanOtherMovieSuccess(getAllFolderPath(context, path));
                    }
                });
            }
        });
    }

    @Override
    public void scanMultiMovies(final Context context, final String[] paths, final int position, final OnScanMultiMovieListener l) {

        if (paths == null || paths.length == 0) {
            l.onScanMultiMovieFailure("path array is null or size is 0");
            return;
        }
        if (paths.length < position) {
            l.onScanMultiMovieFailure("wrong position " + position + " with paths size is " + paths.length);
            return;
        }

        final int[] curPosition = {0};
        ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
            @Override
            public void doSomeThing() {
                final ArrayList<String> result = new ArrayList<>();
                for (int i = 0; i < paths.length; i++) {
                    if (i <= position)
                        curPosition[0] = result.size();
                    String path = paths[i];

                    result.addAll(getAllFolderPath(context, path));
                }

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        l.onScanMultiMovieSuccess(result, curPosition[0]);
                    }
                });
            }
        });
    }

    private ArrayList<String> getAllFolderPath(Context context, String path) {
        ScanUtils scanUtils = new ScanUtils(context);
        ArrayList<String> video =
                scanUtils.getVideo(new File(path).getParent());
        ArrayList<String> prefix = SortUtils.getAllPrefix(video, path);
        ArrayList<String> suffix = SortUtils.getAllSubfix(video, path);
        String sub = suffix.size() > 0 ? suffix.get(suffix.size() - 1) : "";
        String pre = prefix.size() > 0 ? prefix.get(0) : "";

        if (video.size() > 1) {
            Collections.sort(video, new Sort(Sort.UP, pre, sub));
            ArrayList<String> files = SortUtils.getOtherFile(video, path);
            if (files != null && files.size() > 0) {
                video.addAll(files);
            }
        }

        return video;
    }

    public interface OnGetApkList {
        void onGetApkListSuccess(ArrayList<HashMap<String, Object>> result);
    }

    public interface OnUpdateClazzListener {
        void onUpdateClazzSuccess();
    }

    public interface OnScanOtherMovieListener {
        void onScanOtherMovieSuccess(ArrayList<String> result);
    }

    public interface OnScanMultiMovieListener {
        void onScanMultiMovieSuccess(ArrayList<String> result, int position);

        void onScanMultiMovieFailure(String msg);
    }
}