361 lines
13 KiB
Java
361 lines
13 KiB
Java
package com.gh.gamecenter;
|
||
|
||
import android.content.Context;
|
||
import android.content.IntentFilter;
|
||
import android.content.SharedPreferences;
|
||
import android.graphics.Color;
|
||
import android.os.Bundle;
|
||
import android.support.v4.content.ContextCompat;
|
||
import android.text.Html;
|
||
import android.text.TextUtils;
|
||
import android.view.View;
|
||
import android.widget.ImageView;
|
||
import android.widget.LinearLayout;
|
||
import android.widget.ProgressBar;
|
||
import android.widget.TextView;
|
||
|
||
import com.gh.base.AppController;
|
||
import com.gh.base.BaseActivity;
|
||
import com.gh.common.constant.Config;
|
||
import com.gh.common.util.DialogUtils;
|
||
import com.gh.common.util.RandomUtils;
|
||
import com.gh.common.util.Utils;
|
||
import com.gh.gamecenter.entity.KcWebRequestEntity;
|
||
import com.gh.gamecenter.kuaichuan.Constant;
|
||
import com.gh.gamecenter.kuaichuan.DownloadUriHandler;
|
||
import com.gh.gamecenter.kuaichuan.HotspotManager;
|
||
import com.gh.gamecenter.kuaichuan.HtmlUriHandler;
|
||
import com.gh.gamecenter.kuaichuan.ImageUriHandler;
|
||
import com.gh.gamecenter.kuaichuan.KcUriHandler;
|
||
import com.gh.gamecenter.kuaichuan.WifiMgr;
|
||
import com.gh.gamecenter.receiver.WifiAPBroadcastReceiver;
|
||
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.net.InetSocketAddress;
|
||
import java.net.ServerSocket;
|
||
import java.net.Socket;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import butterknife.BindView;
|
||
import butterknife.ButterKnife;
|
||
import butterknife.OnClick;
|
||
|
||
/**
|
||
* Created by khy on 2017/2/6.
|
||
*/
|
||
public class ShareGhWfifActivity extends BaseActivity {
|
||
|
||
// @BindView(R.id.hotspot_name) TextView mHotSpotName;
|
||
@BindView(R.id.init_hotpost_pb)
|
||
ProgressBar mInitHotspostPb;
|
||
@BindView(R.id.init_status_icon)
|
||
ImageView mInitStatusIcon;
|
||
@BindView(R.id.init_status_tv)
|
||
TextView mInitStatusTv;
|
||
@BindView(R.id.init_hotpost_hint)
|
||
TextView mInitHotpostHint;
|
||
@BindView(R.id.init_success_ll)
|
||
LinearLayout mInitSuccessLl;
|
||
@BindView(R.id.init_conn_hint)
|
||
TextView initConnHint;
|
||
|
||
private ServerSocket mServerSocket;
|
||
|
||
private WifiAPBroadcastReceiver mWifiAPBroadcastReceiver;
|
||
|
||
private boolean mIsInitialized = false;
|
||
|
||
private List<KcUriHandler> mUriHandlers;
|
||
|
||
private SharedPreferences sp;
|
||
|
||
private boolean isColseActivity;
|
||
|
||
private boolean isOpenWifi; //记录开热点前的WiFi状态
|
||
|
||
private boolean initSuccess;
|
||
|
||
private String mySsid;
|
||
|
||
@Override
|
||
protected int getLayoutId() {
|
||
return R.layout.activity_share_gh_wifi;
|
||
}
|
||
|
||
@Override
|
||
public void onBackPressed() {
|
||
backActivity();
|
||
}
|
||
|
||
private void backActivity() {
|
||
DialogUtils.showWarningDialog(this, "退出分享", "退出本页面即会关闭分享热点,确定退出吗?"
|
||
, "取消", "确定"
|
||
, new DialogUtils.ConfirmListener() {
|
||
@Override
|
||
public void onConfirm() {
|
||
isColseActivity = true;
|
||
unregisterReceiver(mWifiAPBroadcastReceiver);
|
||
try {
|
||
if (mWifiAPBroadcastReceiver != null) {
|
||
unregisterReceiver(mWifiAPBroadcastReceiver);
|
||
mWifiAPBroadcastReceiver = null;
|
||
}
|
||
} catch (Exception e) {
|
||
Utils.log("网页传发送异常-关闭广播");
|
||
e.printStackTrace();
|
||
}
|
||
|
||
|
||
for (KcUriHandler uriHandler : mUriHandlers) {
|
||
uriHandler.destroy();
|
||
}
|
||
|
||
if (mServerSocket != null) {
|
||
try {
|
||
mServerSocket.close();
|
||
mServerSocket = null;
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
HotspotManager.initUserAp(getApplicationContext());
|
||
if (isOpenWifi) {
|
||
WifiMgr.getInstance(ShareGhWfifActivity.this).openWifi();
|
||
}
|
||
|
||
finish();
|
||
}
|
||
}, null);
|
||
|
||
}
|
||
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
super.onCreate(savedInstanceState);
|
||
initTitle(getString(R.string.title_share_via_wifi));
|
||
ButterKnife.bind(this);
|
||
|
||
mUriHandlers = new ArrayList<>();
|
||
mUriHandlers.add(new HtmlUriHandler(this));
|
||
mUriHandlers.add(new ImageUriHandler(this));
|
||
mUriHandlers.add(new DownloadUriHandler(this));
|
||
|
||
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
|
||
isColseActivity = false;
|
||
initSuccess = false;
|
||
|
||
if (WifiMgr.getInstance(this).isWifiEnable()) {
|
||
isOpenWifi = true;
|
||
WifiMgr.getInstance(this).closeWifi();
|
||
}
|
||
|
||
HotspotManager.initApData(getApplicationContext()); // 记录原始热点信息
|
||
|
||
initHotSpotView(0);
|
||
initHotSpot();
|
||
}
|
||
|
||
/**
|
||
* 初始化创建热点View
|
||
*
|
||
* @param i i=0 初始化中, i=1 初始化成功, i=2 初始化失败
|
||
*/
|
||
|
||
public void initHotSpotView(int i) {
|
||
switch (i) {
|
||
case 0:
|
||
mInitHotspostPb.setVisibility(View.VISIBLE);
|
||
mInitStatusIcon.setVisibility(View.GONE);
|
||
mInitStatusTv.setText("正在创建热点...");
|
||
mInitStatusTv.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.title));
|
||
mInitHotpostHint.setVisibility(View.VISIBLE);
|
||
mInitHotpostHint.setText("如果出现获取权限的提示,请点击允许");
|
||
break;
|
||
case 1:
|
||
mInitStatusIcon.setVisibility(View.VISIBLE);
|
||
mInitStatusIcon.setImageResource(R.drawable.kc_checkbox_select);
|
||
mInitStatusTv.setText(Html.fromHtml("热点创建成功:" + "<font color=\"#00B7FA\">" + mySsid + "</font>"));
|
||
mInitHotpostHint.setVisibility(View.VISIBLE);
|
||
mInitHotpostHint.setText("为了避免消耗流量,请关闭你手机的移动网络");
|
||
mInitHotspostPb.setVisibility(View.GONE);
|
||
|
||
initConnHint.setText(Html.fromHtml("请好友打开Wi-Fi设置,连接我的热点:" + "<font color=\"#00B7FA\">" + mySsid + "</font>"));
|
||
mInitSuccessLl.setVisibility(View.VISIBLE);
|
||
initSuccess = true;
|
||
break;
|
||
case 2:
|
||
mInitStatusIcon.setVisibility(View.VISIBLE);
|
||
mInitStatusIcon.setImageResource(R.drawable.hotspot_failed_icon);
|
||
mInitStatusTv.setText("初始化失败, 请退出重试");
|
||
mInitStatusTv.setTextColor(Color.parseColor("#ff4147"));
|
||
mInitHotpostHint.setVisibility(View.VISIBLE);
|
||
mInitHotspostPb.setVisibility(View.GONE);
|
||
break;
|
||
}
|
||
}
|
||
|
||
private void initHotSpot() {
|
||
|
||
mWifiAPBroadcastReceiver = new WifiAPBroadcastReceiver() {
|
||
|
||
@Override
|
||
public void onWifiApEnabled() {
|
||
if (!mIsInitialized) {
|
||
Utils.log("===初始化热点成功");
|
||
AppController.MAIN_EXECUTOR.execute(checkHotSpot());
|
||
mIsInitialized = true;
|
||
// mHotSpotName.setText(mySsid);
|
||
}
|
||
|
||
}
|
||
};
|
||
|
||
IntentFilter filter = new IntentFilter(WifiAPBroadcastReceiver.ACTION_WIFI_AP_STATE_CHANGED);
|
||
registerReceiver(mWifiAPBroadcastReceiver, filter);
|
||
|
||
HotspotManager.isApOn(getApplicationContext());
|
||
|
||
mySsid = sp.getString("hotspotName", null);
|
||
if (TextUtils.isEmpty(mySsid)) {
|
||
String chars = "abcdefghijklmnopqrstuvwxyz";
|
||
int[] randomArray = RandomUtils.getRandomArray(2, 25);
|
||
|
||
mySsid = "ghZS-";
|
||
for (int i : randomArray) {
|
||
mySsid = mySsid + chars.charAt(i);
|
||
}
|
||
|
||
int default_user_icon = sp.getInt("default_user_icon", 0);
|
||
if (default_user_icon == 0) {
|
||
default_user_icon = RandomUtils.nextInt(8) + 1;
|
||
sp.edit().putInt("default_user_icon", default_user_icon).apply();
|
||
}
|
||
|
||
mySsid = mySsid + default_user_icon;
|
||
sp.edit().putString("hotspotName", mySsid).apply();
|
||
}
|
||
HotspotManager.configApState(getApplicationContext(), mySsid); // change Ap state :boolean
|
||
}
|
||
|
||
private Runnable checkHotSpot() {
|
||
return new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
// 确保热点开启之后获取得到IP地址
|
||
String hotspotIpAddr = WifiMgr.getInstance(ShareGhWfifActivity.this).getHotspotLocalIpAddress();
|
||
int count = 0;
|
||
while (hotspotIpAddr.equals(Constant.DEFAULT_UNKOWN_IP) && count < Constant.DEFAULT_TRY_TIME) {
|
||
Thread.sleep(1000);
|
||
hotspotIpAddr = WifiMgr.getInstance(ShareGhWfifActivity.this).getIpAddressFromHotspot();
|
||
count++;
|
||
}
|
||
|
||
// 即使热点wifi的IP地址也是无法连接网络 所以采取此策略
|
||
// count = 0;
|
||
// while(!WifiUtils.pingIpAddress(hotspotIpAddr) && count < Constant.DEFAULT_TRY_TIME){
|
||
// Thread.sleep(500);
|
||
// count ++;
|
||
// }
|
||
} catch (Exception e) {
|
||
Utils.log("==热点启动异常::" + e.toString());
|
||
}
|
||
|
||
Utils.log("===热点可用");
|
||
|
||
initMicroServer();
|
||
}
|
||
};
|
||
}
|
||
|
||
private void initMicroServer() {
|
||
AppController.MAIN_EXECUTOR.execute(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
try {
|
||
Utils.log("===准备开启微型服务器");
|
||
if (mServerSocket == null) {
|
||
mServerSocket = new ServerSocket();
|
||
mServerSocket.setReuseAddress(true);
|
||
mServerSocket.bind(new InetSocketAddress(Constant.DEFAULT_MICRO_SERVER_PORT));
|
||
}
|
||
|
||
while (!isColseActivity) {
|
||
if (!initSuccess) {
|
||
mInitStatusTv.post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
initHotSpotView(1);
|
||
}
|
||
});
|
||
}
|
||
|
||
Utils.log("===循环等待客户端请求");
|
||
Socket socket = mServerSocket.accept();
|
||
KcWebRequestEntity requestEntity = hanlderSocket(socket);
|
||
for (KcUriHandler uriHandler : mUriHandlers) {
|
||
if (!uriHandler.matches(requestEntity.getUri())) {
|
||
continue;
|
||
}
|
||
|
||
Utils.log("====请求处理::" + requestEntity.getUri() + "==" + uriHandler.getClass().toString());
|
||
uriHandler.handler(requestEntity);
|
||
}
|
||
|
||
}
|
||
|
||
} catch (IOException e) {
|
||
Utils.log("===网页传下载出现异常" + e.toString());
|
||
e.printStackTrace();
|
||
|
||
if (!e.toString().contains("Socket closed")) {
|
||
mInitStatusTv.post(new Runnable() {
|
||
@Override
|
||
public void run() {
|
||
initHotSpotView(2);
|
||
}
|
||
});
|
||
}
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
private KcWebRequestEntity hanlderSocket(Socket socket) {
|
||
KcWebRequestEntity requestEntity = new KcWebRequestEntity();
|
||
|
||
requestEntity.setSocket(socket);
|
||
|
||
try {
|
||
InputStream is = socket.getInputStream();
|
||
StringBuilder sb = new StringBuilder();
|
||
int a = 0, b = 0;
|
||
while ((b != -1) && !(a == '\r' && b == '\n')) {
|
||
a = b;
|
||
b = is.read();
|
||
sb.append((char) (b));
|
||
}
|
||
String[] split = sb.toString().split(" ");
|
||
if (split.length > 1) {
|
||
String requestUri = split[1];
|
||
requestEntity.setUri(requestUri);
|
||
Utils.log("===客户端请求链接::" + requestUri);
|
||
} else {
|
||
requestEntity.setUri("/ERROR");
|
||
Utils.log("===客户端请求链接异常::" + sb.toString());
|
||
}
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return requestEntity;
|
||
}
|
||
|
||
@OnClick(R.id.actionbar_rl_back)
|
||
public void OnBackClikListener() {
|
||
backActivity();
|
||
}
|
||
}
|