parent
287719d550
commit
ad4e5af7c7
@ -0,0 +1,66 @@
|
|||||||
|
package com.monke.monkeybook;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
|
||||||
|
import org.jsoup.helper.StringUtil;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class ProxyManager {
|
||||||
|
public static final String SP_KEY_PROXY_HTTP = "proxy_http";
|
||||||
|
public static final String SP_KEY_PROXY_STATE = "proxy_state";
|
||||||
|
public static final String PROXY_HTTP_DEFAULT = "";
|
||||||
|
public static final boolean PROXY_STATE_DEFAULT = false;
|
||||||
|
|
||||||
|
public static boolean proxyState;
|
||||||
|
public static String proxyHttp;
|
||||||
|
private static final String proxyHttpMatch = "(http|ftp|https):\\/\\/[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?";//http正则表达式
|
||||||
|
private static final String encode = "代理包名加密key";
|
||||||
|
public static String packAgeEncode; //加密后的包名
|
||||||
|
public static void saveProxyState(boolean state){
|
||||||
|
proxyState = state;
|
||||||
|
SharedPreferences.Editor editor = MApplication.getInstance().getSharedPreferences("CONFIG", 0).edit();
|
||||||
|
editor.putBoolean(SP_KEY_PROXY_STATE,proxyState);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initProxyState(){
|
||||||
|
try {
|
||||||
|
packAgeEncode = MApplication.getInstance().getPackageManager().getPackageInfo(MApplication.getInstance().getPackageName(), 0).packageName;
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("=================包名获取失败,可能会影响代理请求功能");
|
||||||
|
}
|
||||||
|
proxyState = MApplication.getInstance().getSharedPreferences("CONFIG", 0).getBoolean(SP_KEY_PROXY_STATE,PROXY_STATE_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void saveProxyHttp(String http){
|
||||||
|
proxyHttp = http;
|
||||||
|
SharedPreferences.Editor editor = MApplication.getInstance().getSharedPreferences("CONFIG", 0).edit();
|
||||||
|
editor.putString(SP_KEY_PROXY_HTTP,proxyHttp);
|
||||||
|
editor.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void initProxyHttp(){
|
||||||
|
proxyHttp = MApplication.getInstance().getSharedPreferences("CONFIG", 0).getString(SP_KEY_PROXY_HTTP,PROXY_HTTP_DEFAULT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void initProxy(){
|
||||||
|
initProxyHttp();
|
||||||
|
initProxyState();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean hasProxy(){
|
||||||
|
if(!proxyState){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Pattern pattern = Pattern.compile(proxyHttpMatch);
|
||||||
|
if(!StringUtil.isBlank(proxyHttp) && pattern.matcher(proxyHttp).matches()){
|
||||||
|
return true;
|
||||||
|
}else{
|
||||||
|
saveProxyState(false);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.monke.monkeybook.base;
|
||||||
|
|
||||||
|
import com.monke.basemvplib.EncodoConverter;
|
||||||
|
import com.monke.basemvplib.impl.RetryIntercepter;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||||
|
import retrofit2.converter.scalars.ScalarsConverterFactory;
|
||||||
|
|
||||||
|
public class MBaseModelImpl {
|
||||||
|
protected OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
|
||||||
|
.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
|
.writeTimeout(10, TimeUnit.SECONDS)
|
||||||
|
.readTimeout(10, TimeUnit.SECONDS)
|
||||||
|
.addNetworkInterceptor(new ProxyInterceptor())
|
||||||
|
.addInterceptor(new RetryIntercepter(1));
|
||||||
|
|
||||||
|
protected Retrofit getRetrofitObject(String url) {
|
||||||
|
return new Retrofit.Builder().baseUrl(url)
|
||||||
|
//增加返回值为字符串的支持(以实体类返回)
|
||||||
|
.addConverterFactory(ScalarsConverterFactory.create())
|
||||||
|
//增加返回值为Oservable<T>的支持
|
||||||
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||||
|
.client(clientBuilder.build())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Retrofit getRetrofitString(String url, String encode) {
|
||||||
|
return new Retrofit.Builder().baseUrl(url)
|
||||||
|
//增加返回值为字符串的支持(以实体类返回)
|
||||||
|
.addConverterFactory(EncodoConverter.create(encode))
|
||||||
|
//增加返回值为Oservable<T>的支持
|
||||||
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||||
|
.client(clientBuilder.build())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
package com.monke.monkeybook.base;
|
||||||
|
|
||||||
|
import com.monke.monkeybook.ProxyManager;
|
||||||
|
|
||||||
|
import org.jsoup.helper.StringUtil;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
|
||||||
|
import okhttp3.Interceptor;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class ProxyInterceptor implements Interceptor {
|
||||||
|
public ProxyInterceptor() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Response intercept(Chain chain) throws IOException {
|
||||||
|
Request request = chain.request();
|
||||||
|
if (ProxyManager.hasProxy()) { //如果是代理模式则优先请求代理服务器,失败再自行本地请求
|
||||||
|
String url = request.url().toString();
|
||||||
|
if (!StringUtil.isBlank(url)) {
|
||||||
|
url = URLEncoder.encode("url", "utf-8");
|
||||||
|
}
|
||||||
|
Request requestProxy = new Request.Builder()
|
||||||
|
.url(ProxyManager.proxyHttp)
|
||||||
|
.header("url", url)
|
||||||
|
.header("packagename",ProxyManager.packAgeEncode)
|
||||||
|
.get()
|
||||||
|
.build();
|
||||||
|
Response responseProxy = chain.proceed(request);
|
||||||
|
if(responseProxy.isSuccessful()){
|
||||||
|
return responseProxy;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Response response = chain.proceed(request);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,112 @@
|
|||||||
|
package com.monke.monkeybook.view.popupwindow;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.view.inputmethod.EditorInfo;
|
||||||
|
import android.view.inputmethod.InputMethodManager;
|
||||||
|
import android.widget.CompoundButton;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
import android.widget.PopupWindow;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import com.daimajia.androidanimations.library.Techniques;
|
||||||
|
import com.daimajia.androidanimations.library.YoYo;
|
||||||
|
import com.kyleduo.switchbutton.SwitchButton;
|
||||||
|
import com.monke.monkeybook.ProxyManager;
|
||||||
|
import com.monke.monkeybook.R;
|
||||||
|
|
||||||
|
public class ProxyPop extends PopupWindow {
|
||||||
|
private Context mContext;
|
||||||
|
private View view;
|
||||||
|
|
||||||
|
private FrameLayout flProxyContent;
|
||||||
|
private EditText edtProxyHttp;
|
||||||
|
private SwitchButton sbProxyStart;
|
||||||
|
|
||||||
|
public ProxyPop(Context context) {
|
||||||
|
super(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||||
|
mContext = context;
|
||||||
|
view = LayoutInflater.from(mContext).inflate(R.layout.view_pop_proxy, null);
|
||||||
|
this.setContentView(view);
|
||||||
|
bindView();
|
||||||
|
bindEvent();
|
||||||
|
|
||||||
|
setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_pop_checkaddshelf_bg));
|
||||||
|
setFocusable(true);
|
||||||
|
setTouchable(true);
|
||||||
|
setAnimationStyle(R.style.anim_pop_checkaddshelf);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindEvent() {
|
||||||
|
edtProxyHttp.setOnFocusChangeListener(new View.OnFocusChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onFocusChange(View v, boolean hasFocus) {
|
||||||
|
if (!hasFocus) {
|
||||||
|
finishInputProxyHttp(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
edtProxyHttp.setOnEditorActionListener(new TextView.OnEditorActionListener() {
|
||||||
|
@Override
|
||||||
|
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
|
||||||
|
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||||
|
finishInputProxyHttp(true);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
flProxyContent.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
finishInputProxyHttp(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
edtProxyHttp.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
edtProxyHttp.setCursorVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
sbProxyStart.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
|
||||||
|
@Override
|
||||||
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
||||||
|
finishInputProxyHttp(false);
|
||||||
|
ProxyManager.saveProxyState(isChecked);
|
||||||
|
if (isChecked) {
|
||||||
|
if (!ProxyManager.hasProxy()) {
|
||||||
|
YoYo.with(Techniques.Shake).playOn(edtProxyHttp);
|
||||||
|
sbProxyStart.setCheckedImmediatelyNoEvent(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void bindView() {
|
||||||
|
edtProxyHttp = view.findViewById(R.id.edt_proxy_http);
|
||||||
|
sbProxyStart = view.findViewById(R.id.sb_proxy_start);
|
||||||
|
flProxyContent = view.findViewById(R.id.fl_proxy_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void showAsDropDown(View anchor) {
|
||||||
|
edtProxyHttp.setText(ProxyManager.proxyHttp);
|
||||||
|
sbProxyStart.setCheckedImmediatelyNoEvent(ProxyManager.proxyState);
|
||||||
|
super.showAsDropDown(anchor);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishInputProxyHttp(boolean needUpdateState) {
|
||||||
|
ProxyManager.saveProxyHttp(edtProxyHttp.getText().toString().trim());
|
||||||
|
if (needUpdateState) {
|
||||||
|
sbProxyStart.setCheckedImmediatelyNoEvent(ProxyManager.hasProxy());
|
||||||
|
}
|
||||||
|
edtProxyHttp.setCursorVisible(false);
|
||||||
|
InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||||
|
imm.hideSoftInputFromWindow(edtProxyHttp.getWindowToken(), 0);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||||
|
<FrameLayout
|
||||||
|
android:id="@+id/fl_proxy_content"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:minHeight="80dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:paddingBottom="16.5dp"
|
||||||
|
android:paddingTop="3.5dp"
|
||||||
|
android:paddingLeft="9.5dp"
|
||||||
|
android:paddingRight="10.5dp"
|
||||||
|
android:background="@drawable/bg_search">
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:layout_gravity="top|center_horizontal">
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:paddingTop="10dp"
|
||||||
|
android:paddingLeft="10dp"
|
||||||
|
android:paddingRight="10dp"
|
||||||
|
android:paddingBottom="5dp">
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="网络请求代理设置"
|
||||||
|
android:textColor="#737373"/>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textColor="#999999"
|
||||||
|
android:textSize="12sp"
|
||||||
|
android:text="(谨慎设置)"
|
||||||
|
android:layout_marginLeft="10dp"/>
|
||||||
|
</LinearLayout>
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:paddingTop="5dp"
|
||||||
|
android:paddingLeft="5dp"
|
||||||
|
android:paddingRight="5dp"
|
||||||
|
android:paddingBottom="10dp">
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/edt_proxy_http"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:layout_height="30dp"
|
||||||
|
android:background="@drawable/bg_search_content"
|
||||||
|
android:gravity="center_vertical|left"
|
||||||
|
android:paddingLeft="10dp"
|
||||||
|
android:paddingRight="10dp"
|
||||||
|
android:hint="http://"
|
||||||
|
android:cursorVisible="false"
|
||||||
|
android:textColorHint="#6f6f6f"
|
||||||
|
android:textSize="13sp"
|
||||||
|
android:lines="1"
|
||||||
|
android:maxLines="1"
|
||||||
|
android:inputType="text"
|
||||||
|
android:imeOptions="actionDone"
|
||||||
|
android:textColor="#767676"
|
||||||
|
android:textCursorDrawable="@drawable/shape_text_cursor"/>
|
||||||
|
<TextView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="开启代理"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:layout_marginLeft="10dp"
|
||||||
|
android:layout_marginRight="5dp"
|
||||||
|
android:textSize="12sp"
|
||||||
|
android:textColor="#737373"/>
|
||||||
|
<com.kyleduo.switchbutton.SwitchButton
|
||||||
|
android:id="@+id/sb_proxy_start"
|
||||||
|
android:layout_width="47dp"
|
||||||
|
android:layout_height="26dp"
|
||||||
|
app:kswThumbColor="#ffffff"
|
||||||
|
app:kswBackColor="@drawable/selector_switchbutton"
|
||||||
|
android:layout_gravity="center_vertical"/>
|
||||||
|
</LinearLayout>
|
||||||
|
</LinearLayout>
|
||||||
|
<View
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="0.7dp"
|
||||||
|
android:background="#c1c1c1"
|
||||||
|
android:layout_gravity="bottom"/>
|
||||||
|
</FrameLayout>
|
||||||
|
</FrameLayout>
|
Loading…
Reference in new issue