AskService.java 2.93 KB
package com.xgimi.gimicinema.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;
import com.gimi.common.cinema.db.NewDBManager;
import com.gimi.common.cinema.model.Constant;
import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.utils.LocalDataUtils;
import com.gimi.common.cinema.utils.OpenMMUtils;
import com.gimi.common.cinema.utils.SystemUtils;
import com.gimi.common.cinema.utils.Utils;
import com.xgimi.gimicinema.mview.IAskView;
import com.xgimi.gimicinema.presenter.AskPresenter;

import java.util.List;


/**
 * Created by pc on 2015/1/6
 */
public class AskService extends Service implements IAskView {
    public static final String TAG = "AskService";
    private Handler handler = new Handler();

    private Context context;
    private SystemUtils systemUtils;
    private SharedPreferences sharedPreferences;
    private NewDBManager dbManager;
    private LocalDataUtils localDataUtils;
    private AskPresenter askPresenter;
    private String ip = "";
    private String folder = "";

    private SambaMsg sambaMsg;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        askPresenter = new AskPresenter(this);
        localDataUtils = new LocalDataUtils(context);
        dbManager = new NewDBManager(this);
        systemUtils = new SystemUtils();
        sharedPreferences = Utils.getSp(context);
        agentType = Utils.getInt(sharedPreferences, "agent-type", 0);
        updateMsg();
        askPresenter.load(context);
        handler.postDelayed(updateRunnable, 5 * 1000);
    }

    private void updateMsg() {
        sambaMsg = Utils.getSambaMsg(sharedPreferences);
        ip = sambaMsg.getIp();
        folder = sambaMsg.getFolder();
    }

    int agentType;

    private String currentPath = "";

    private Runnable updateRunnable = new Runnable() {
        @Override
        public void run() {
            Constant.gimiAuth = sharedPreferences.getBoolean(Constant.GIMI_AUTHENTICATION, true);
            if (Constant.gimiAuth) {
                localDataUtils.updateDb();
            }
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new NullPointerException();
    }

    @Override
    public void onDestroy() {
        askPresenter.umountSamba();
        super.onDestroy();
    }

    @Override
    public void playPath() {
        OpenMMUtils.openMM(context, currentPath, null);
    }

    @Override
    public void playPath(List<String> result, int position) {
        OpenMMUtils.openMM(context, null, result, null, position);
    }

    @Override
    public void showMsg(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }
}