Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: 允许自定义用户代理列表 #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class PreferenceKey {
public static final String BLACK_LIST = "";
public static final String FILTER_KEYWORDS_LIST = "filter_keywords";

public static final String USER_AGENT_LIST = "user_agents";

public static final String SHOW_ICON_MODE = "showiconmode";

public static final String ADJUST_SIZE = "adjust_size";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import gov.anzong.androidnga.common.util.ReflectUtils;
import gov.anzong.androidnga.db.AppDatabase;
import sp.phone.common.FilterKeywordsManagerImpl;
import sp.phone.common.UserAgentManagerImpl;
import sp.phone.common.UserManagerImpl;
import sp.phone.common.VersionUpgradeHelper;
import sp.phone.util.NLog;
Expand Down Expand Up @@ -89,6 +90,7 @@ private void initRouter() {
private void initCoreModule() {
UserManagerImpl.getInstance().initialize(this);
FilterKeywordsManagerImpl.getInstance().initialize(this);
UserAgentManagerImpl.getInstance().initialize(this);
// // 注册crashHandler
// CrashHandler.getInstance().init(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;

Expand Down
19 changes: 19 additions & 0 deletions nga_phone_base_3.0/src/main/java/sp/phone/common/UserAgent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sp.phone.common;

public class UserAgent {
private String keyword;
private boolean enabled;

public UserAgent() {}

public UserAgent(String keyword) {
this.keyword = keyword;
this.enabled = true;
}

public String getKeyword() { return keyword; }
public void setKeyword(String keyword) { this.keyword = keyword; }
public boolean isEnabled() { return enabled; }
public void setEnabled(boolean enabled) { this.enabled = enabled; }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sp.phone.common;

import android.content.Context;

import java.util.List;

public interface UserAgentManager {
void initialize(Context context);
void toggleUserAgent(int position);

void addUserAgent(UserAgent keyword);

List<UserAgent> getUserAgents();
void removeUserAgent(int index);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package sp.phone.common;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

import gov.anzong.androidnga.common.PreferenceKey;

public class UserAgentManagerImpl implements UserAgentManager {

private SharedPreferences mPrefs;

private List<UserAgent> mUserAgents;

private static class SingletonHolder {
static UserAgentManagerImpl sInstance = new UserAgentManagerImpl();
}

public static UserAgentManagerImpl getInstance() {
return SingletonHolder.sInstance;
}

private UserAgentManagerImpl() {
}

@Override
public void initialize(Context context) {
Context mContext = context.getApplicationContext();
mPrefs = PreferenceManager.getDefaultSharedPreferences(context);

String uaListStr = mPrefs.getString(PreferenceKey.USER_AGENT_LIST, "");
Log.d("uaListStr",uaListStr);
if (TextUtils.isEmpty(uaListStr)) {
mUserAgents = new ArrayList<>();
} else {
mUserAgents = JSON.parseArray(uaListStr, UserAgent.class);
if (mUserAgents == null) {
mUserAgents = new ArrayList<>();
}
}
if(mUserAgents.size()==0){
UserAgent ua = new UserAgent("自动");
ua.setEnabled(true);
mUserAgents.add(0,ua);
}
versionUpgrade();
}

private void versionUpgrade() {
}

@Override
public void toggleUserAgent(int position) {
UserAgent userAgent = mUserAgents.get(position);
userAgent.setEnabled(true);
closeOthers(position);
commit();
}

public void closeOthers(UserAgent userAgent){
mUserAgents.forEach((ua)->{
if(ua!=userAgent&&ua.isEnabled()){
ua.setEnabled(false);
}
});
}

public void closeOthers(int pos){
UserAgent userAgent = mUserAgents.get(pos);
mUserAgents.forEach((ua)->{
if(ua!=userAgent&&ua.isEnabled()){
ua.setEnabled(false);
}
});
}

@Override
public void addUserAgent(UserAgent userAgent) {
mUserAgents.add(userAgent);
closeOthers(userAgent);
commit();
}

@Override
public List<UserAgent> getUserAgents() {
return mUserAgents;
}

@Override
public void removeUserAgent(int index) {
if(index>0){
if(mUserAgents.get(index).isEnabled()){
mUserAgents.get(0).setEnabled(true);
}
mUserAgents.remove(index);
commit();
}
}

private void commit() {
mPrefs.edit()
.putString(PreferenceKey.USER_AGENT_LIST, JSON.toJSONString(mUserAgents))
.apply();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import java.net.URLDecoder;
import java.util.Objects;

import gov.anzong.androidnga.base.util.ContextUtils;
import gov.anzong.androidnga.base.util.StringUtils;
Expand Down Expand Up @@ -32,34 +34,47 @@ public class RetrofitHelper {

private String mBaseUrl;

private String mUserAgent;

private RetrofitHelper() {
SharedPreferences sp = ContextUtils.getContext().getSharedPreferences(PreferenceKey.PERFERENCE, Context.MODE_PRIVATE);
mBaseUrl = ForumUtils.getAvailableDomain();
mUserAgent = ForumUtils.getCurrentUserAgent();
mRetrofit = createRetrofit();

sp.registerOnSharedPreferenceChangeListener((sp1, key) -> {
if (key.equals(PreferenceKey.KEY_NGA_DOMAIN)) {
mBaseUrl = ForumUtils.getAvailableDomain();
mRetrofit = createRetrofit();
}
if (key.equals(PreferenceKey.USER_AGENT_LIST)) {
mUserAgent = ForumUtils.getCurrentUserAgent();
mRetrofit = createRetrofit();
}
});
}

public void updateRetrofit(){
mBaseUrl = ForumUtils.getAvailableDomain();
mUserAgent = ForumUtils.getCurrentUserAgent();
mRetrofit = createRetrofit();
}

public Retrofit createRetrofit() {
return createRetrofit(mBaseUrl, null);
return createRetrofit(mBaseUrl,mUserAgent, null);
}

public Retrofit createRetrofit(String baseUrl) {
return createRetrofit(baseUrl, null);
return createRetrofit(baseUrl, mUserAgent,null);
}

public Retrofit createRetrofit(OkHttpClient.Builder builder) {
return createRetrofit(mBaseUrl, builder);
return createRetrofit(mBaseUrl, mUserAgent,builder);
}

public Retrofit createRetrofit(String baseUrl, OkHttpClient.Builder builder) {
public Retrofit createRetrofit(String baseUrl,String userAgent, OkHttpClient.Builder builder) {
if (builder == null) {
builder = createOkHttpClientBuilder();
builder = createOkHttpClientBuilder(userAgent);
}
return new Retrofit.Builder()
.baseUrl(baseUrl)
Expand All @@ -69,7 +84,7 @@ public Retrofit createRetrofit(String baseUrl, OkHttpClient.Builder builder) {
.build();
}

public OkHttpClient.Builder createOkHttpClientBuilder() {
public OkHttpClient.Builder createOkHttpClientBuilder(String userAgent) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(chain -> {
Request original = chain.request();
Expand All @@ -80,7 +95,7 @@ public OkHttpClient.Builder createOkHttpClientBuilder() {
}
Request request = original.newBuilder()
.header("Cookie", cookie)
.header("User-Agent", "Nga_Official/80023")
.header("User-Agent", Objects.equals(userAgent, "自动") ?"Nga_Official/80023":userAgent)
.method(original.method(), original.body())
.build();
return chain.proceed(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import sp.phone.param.PostParam;
import sp.phone.rxjava.BaseSubscriber;
import sp.phone.task.TopicPostTask;
import sp.phone.util.ForumUtils;
import sp.phone.util.ImageUtils;
import sp.phone.util.NLog;
import sp.phone.util.StringUtils;
Expand All @@ -57,12 +58,14 @@ public class TopicPostModel extends BaseModel implements TopicPostContract.Model

private String mHostUrl = Utils.getNGAHost() + "post.php?";

private String mUserAgent = ForumUtils.getCurrentUserAgent();

private static final String BASE_URL_ATTACHMENT_SERVER = "https://img8.nga.cn/attach.php?";

private RetrofitService mRetrofitService;

public TopicPostModel() {
OkHttpClient.Builder builder = RetrofitHelper.getInstance().createOkHttpClientBuilder();
OkHttpClient.Builder builder = RetrofitHelper.getInstance().createOkHttpClientBuilder(mUserAgent);
builder.connectTimeout(5, TimeUnit.MINUTES);
mRetrofitService = RetrofitHelper.getInstance().createRetrofit(builder).create(RetrofitService.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package sp.phone.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import gov.anzong.androidnga.R;
import sp.phone.common.UserAgent;
import sp.phone.view.RecyclerViewEx;

public class UserAgentsAdapter extends RecyclerViewEx.Adapter<UserAgentsAdapter.UserAgentsViewHodler> {
private Context mContext;

private View.OnClickListener mOnClickListener;

private List<UserAgent> mUserAgents;

public UserAgentsAdapter(Context context, List<UserAgent> userAgents) {
this.mContext = context;
this.mUserAgents = userAgents;
}

@NonNull
@Override
public UserAgentsViewHodler onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
View convertView = LayoutInflater.from(mContext).inflate(R.layout.list_user_agents_item, viewGroup, false);
UserAgentsAdapter.UserAgentsViewHodler viewHolder = new UserAgentsAdapter.UserAgentsViewHodler(convertView);

viewHolder.itemView.setOnClickListener(mOnClickListener);
viewHolder.checkView.setOnClickListener(mOnClickListener);

return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull UserAgentsViewHodler viewHolder, int position) {
UserAgent keyword = mUserAgents.get(position);
viewHolder.userAgentView.setText(keyword.getKeyword());
viewHolder.checkView.setChecked(keyword.isEnabled());
viewHolder.userAgentView.setTag(position);
viewHolder.checkView.setTag(position);
}

@Override
public int getItemCount() {
return mUserAgents.size();
}

public void setOnClickListener(View.OnClickListener onClickListener) {
this.mOnClickListener = onClickListener;
}

public static class UserAgentsViewHodler extends RecyclerView.ViewHolder {

@BindView(R.id.user_agent)
TextView userAgentView;

@BindView(R.id.check)
Switch checkView;

public UserAgentsViewHodler(@NonNull View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
}
Loading