SambaModelImpl.java 5.95 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.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.gimi.common.cinema.model.SambaMsg;
import com.gimi.common.cinema.samba.SambaUtil;
import com.gimi.common.cinema.utils.HimediaResUtils;
import com.gimi.common.cinema.utils.Utils;
import com.mstar.android.samba.OnRecvMsg;
import com.mstar.android.samba.SmbDevice;
import com.xgimi.gimicinema.BuildConfig;
import com.xgimi.gimicinema.activity.CinemaConfig;

/**
 * Created by 李攀 on 2016/9/22
 */
public class SambaModelImpl implements ISambaModel {

    private SambaUtil sambaUtil;
    private Handler handler;

    @Override
    public void umountSamba() {
        if (CinemaConfig.smbDevices == null || CinemaConfig.smbDevices.size() == 0) {
            return;
        }
        for (SmbDevice smbDevice : CinemaConfig.smbDevices) {
            Log.d("lovely", "╟  " + smbDevice.toString());
            try {
                int unmount = smbDevice.unmount();
                Log.d("lovely", "╟unmount:  " + unmount);
                CinemaConfig.smbDevices.remove(smbDevice);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void mountSamba(Context ctx, OnSambaMountListener listener) {
        SambaMsg sambaMsg = Utils.getSambaMsg(ctx);
        mountSamba(ctx, sambaMsg, listener);
    }


    @Override
    public void mountSamba(final Context ctx, final SambaMsg msg, OnSambaMountListener l) {
        if (BuildConfig.MACHINE_TYPE.equals("himedia")) {
            HimediaResUtils.mountSamba(ctx, msg.getIp());

            if (!TextUtils.isEmpty(CinemaConfig.BASIC_ROOT)) {
                l.onSambaMountSuccess();
            }
            return;
        }
        if (sambaUtil == null) {
            sambaUtil = new SambaUtil();
        }
        handler = getMountHandler(l);
        sambaUtil.setHandler(handler);
        ThreadUtils.subThread(new ThreadUtils.DoSomeThing() {
            @Override
            public void doSomeThing() {
                CinemaConfig.BASIC_ROOT = msg.getRootPath();
                try {
                    sambaUtil.mount(ctx, msg);
                } catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        });
    }

    private Handler getMountHandler(final OnSambaMountListener l) {
        return new Handler() {
            @Override
            public void handleMessage(final Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case OnRecvMsg.NT_STATUS_WRONG_PASSWORD:
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                l.onSambaMountFailure(msg.what, "密码错误");
                            }
                        });
                        break;
                    case OnRecvMsg.NT_STATUS_LOGON_FAILURE:
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                l.onSambaMountFailure(msg.what, "登录失败");
                            }
                        });
                        break;
                    case OnRecvMsg.NT_STATUS_OK:
                        if (msg.obj != null) {
                            Log.d("lovely", "╟  msg obj is not null");
                            SmbDevice obj = (SmbDevice) msg.obj;
                            boolean exist = false;
                            String address = obj.getAddress();
                            for (SmbDevice smbDevice : CinemaConfig.smbDevices) {
                                if (smbDevice.getAddress().equals(address)) {
                                    exist = true;
                                }
                            }
                            if (!exist) {
                                CinemaConfig.smbDevices.add(obj);
                                Log.d("lovely", "╟  add" + obj.toString());
                            } else {
                                Log.d("lovely", "╟  add failure");
                            }
                        } else {
                            Log.d("lovely", "╟  msg obj is null");
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                l.onSambaMountSuccess();
                            }
                        });
                        break;
                    case OnRecvMsg.NT_STATUS_NOT_SUPPORT://网络错误
                    case OnRecvMsg.NT_STATUS_UNSUCCESSFUL://
                    default:
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                l.onSambaMountFailure(msg.what,
                                        "未知错误,请检查网络状态及服务器配置");
                            }
                        });
                        break;
                }
            }
        };
    }

    public interface OnSambaMountListener {
        void onSambaMountSuccess();

        void onSambaMountFailure(int code, String wrongMsg);
    }
}