SambaModelImpl.java
5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* 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);
}
}