Skip to content

Commit 43ddf79

Browse files
committed
[ADD] 增加设置,清除浏览器的cookie
1 parent a45f461 commit 43ddf79

File tree

9 files changed

+177
-67
lines changed

9 files changed

+177
-67
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.app.jdcookie">
44

5-
<uses-permission android:name="android.permission.INTERNET"/>
5+
<uses-permission android:name="android.permission.INTERNET" />
66

77
<application
88
android:allowBackup="true"
@@ -11,7 +11,10 @@
1111
android:roundIcon="@mipmap/ic_launcher_round"
1212
android:supportsRtl="true"
1313
android:theme="@style/Theme.JDCookie">
14-
<activity android:name=".MainActivity">
14+
<activity android:name=".activity.SetActivity" />
15+
<activity
16+
android:name=".activity.MainActivity"
17+
android:windowSoftInputMode="adjustPan">
1518
<intent-filter>
1619
<action android:name="android.intent.action.MAIN" />
1720

app/src/main/java/com/app/jdcookie/MainActivity.java renamed to app/src/main/java/com/app/jdcookie/activity/MainActivity.java

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
package com.app.jdcookie;
1+
package com.app.jdcookie.activity;
22

3-
import androidx.annotation.NonNull;
43
import androidx.annotation.Nullable;
54
import androidx.appcompat.app.AppCompatActivity;
65
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -9,15 +8,13 @@
98
import android.content.ClipData;
109
import android.content.ClipboardManager;
1110
import android.content.Context;
11+
import android.content.Intent;
1212
import android.graphics.Bitmap;
13-
import android.net.Uri;
1413
import android.os.Build;
1514
import android.os.Bundle;
1615
import android.text.TextUtils;
17-
import android.util.Log;
1816
import android.view.View;
1917
import android.webkit.CookieManager;
20-
import android.webkit.ValueCallback;
2118
import android.webkit.WebChromeClient;
2219
import android.webkit.WebResourceRequest;
2320
import android.webkit.WebResourceResponse;
@@ -27,25 +24,9 @@
2724
import android.widget.ProgressBar;
2825
import android.widget.Toast;
2926

30-
import com.chad.library.adapter.base.BaseQuickAdapter;
31-
import com.chad.library.adapter.base.listener.OnItemClickListener;
32-
33-
import java.io.BufferedReader;
34-
import java.io.IOException;
35-
import java.io.InputStream;
36-
import java.io.InputStreamReader;
37-
import java.net.HttpURLConnection;
38-
import java.net.URL;
39-
import java.net.URLConnection;
40-
import java.util.Arrays;
41-
import java.util.List;
42-
import java.util.Map;
43-
import java.util.Set;
44-
45-
import okhttp3.Cookie;
46-
import okhttp3.OkHttpClient;
47-
import okhttp3.Request;
48-
import okhttp3.Response;
27+
import com.app.jdcookie.CookieListener;
28+
import com.app.jdcookie.MyAdapter;
29+
import com.app.jdcookie.R;
4930

5031
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
5132

@@ -71,6 +52,7 @@ private void initView() {
7152

7253
findViewById(R.id.main_back).setOnClickListener(this);
7354
findViewById(R.id.main_clear).setOnClickListener(this);
55+
findViewById(R.id.main_set).setOnClickListener(this);
7456
webBridgeProgressBar = findViewById(R.id.main_progress_bar);
7557

7658
recyclerView = findViewById(R.id.main_recycler_view);
@@ -105,15 +87,8 @@ private void initWebView() {
10587
webSetting.setUseWideViewPort(true);
10688

10789
webSetting.setBlockNetworkImage(false);
108-
// webSetting.setUserAgentString(userAgent);
109-
// if (NetWorkUtils.isNetConnected(App.getAppContext())) {
11090
//缓存模式
11191
webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
112-
// } else {
113-
// //缓存模式
114-
// webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
115-
// }
116-
11792
webSetting.setDatabaseEnabled(true);
11893
webSetting.setDomStorageEnabled(true);
11994
webSetting.setAppCacheMaxSize(1024 * 1024 * 8);
@@ -183,7 +158,6 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
183158
//截取到";"前的 pt_key
184159
pt_key = pt_key.substring(0, pt_key.indexOf(";", 1) + 1);
185160

186-
187161
cookieListener.onCookie(cookie, pt_key + pt_pin);
188162
}
189163
} catch (Exception e) {
@@ -205,38 +179,38 @@ public MyWebChromeClient(ProgressBar webBridgeProgressBar) {
205179
public void onProgressChanged(WebView view, int newProgress) {
206180
if (null != webBridgeProgressBar) {
207181
webBridgeProgressBar.setProgress(newProgress);
208-
if (newProgress == 100) {
209-
}
210182
}
211183
super.onProgressChanged(view, newProgress);
212184
}
213185

214186
@Override
215187
public void onReceivedTitle(WebView view, String title) {
216188
super.onReceivedTitle(view, title);
217-
if (!view.getTitle().startsWith("http")) {
218-
// if (webBridgeNormalTitleBar != null) {
219-
// webBridgeNormalTitleBar.setTitleText(view.getTitle());
220-
// }
221-
}
222189
}
223190

224191
}
225192

193+
@Override
194+
public void onBackPressed() {
195+
//返回上一页面
196+
if (webBridgeWebView.canGoBack()) {
197+
webBridgeWebView.goBack();
198+
} else {
199+
onBackPressed();
200+
}
201+
}
202+
226203
@Override
227204
public void onClick(View v) {
228205
if (v.getId() == R.id.main_back) {
229-
//返回上一页面
230-
if (webBridgeWebView.canGoBack()) {
231-
webBridgeWebView.goBack();
232-
} else {
233-
onBackPressed();
234-
}
206+
onBackPressed();
235207
} else if (v.getId() == R.id.main_clear) {
236208
if (adapter != null) {
237209
adapter.getData().clear();
238210
adapter.notifyDataSetChanged();
239211
}
212+
} else if (v.getId() == R.id.main_set) {
213+
startActivity(new Intent(this, SetActivity.class));
240214
}
241215
}
242216
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.app.jdcookie.activity;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
5+
import android.os.Bundle;
6+
import android.view.View;
7+
import android.webkit.CookieManager;
8+
import android.webkit.ValueCallback;
9+
import android.widget.Toast;
10+
11+
import com.app.jdcookie.R;
12+
13+
public class SetActivity extends AppCompatActivity implements View.OnClickListener {
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.activity_set);
19+
20+
21+
findViewById(R.id.set_back_image).setOnClickListener(this);
22+
findViewById(R.id.set_clear_cookie_layout).setOnClickListener(this);
23+
}
24+
25+
@Override
26+
public void onClick(View v) {
27+
if (v.getId() == R.id.set_back_image) {
28+
29+
onBackPressed();
30+
} else if (v.getId() == R.id.set_clear_cookie_layout) {
31+
//清除浏览器的cookie
32+
CookieManager instance = CookieManager.getInstance();
33+
instance.removeAllCookies(value -> Toast.makeText(SetActivity.this, "清除" + (value ? "成功" : "失败"), Toast.LENGTH_SHORT).show());
34+
instance.flush();
35+
}
36+
}
37+
}
525 Bytes
Loading
2.19 KB
Loading

app/src/main/res/layout/activity_main.xml

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,35 @@
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
77
android:orientation="vertical"
8-
tools:context=".MainActivity">
8+
tools:context=".activity.MainActivity">
99

1010
<LinearLayout
1111
android:layout_width="match_parent"
1212
android:layout_height="45dp"
13+
android:background="@color/purple_200"
1314
android:orientation="horizontal">
1415

1516
<ImageView
1617
android:id="@+id/main_back"
1718
android:layout_width="45dp"
1819
android:layout_height="45dp"
1920
android:padding="10dp"
20-
android:src="@drawable/icon_back" />
21+
android:src="@drawable/icon_back"
22+
app:tint="#fff" />
2123

2224
<View
2325
android:layout_width="0dp"
2426
android:layout_height="0dp"
2527
android:layout_weight="1" />
2628

2729
<ImageView
28-
android:id="@+id/main_clear"
30+
android:id="@+id/main_set"
2931
android:layout_width="45dp"
3032
android:layout_height="45dp"
31-
android:padding="10dp"
32-
android:src="@drawable/icon_clear" />
33-
33+
android:layout_alignParentEnd="true"
34+
app:tint="#fff"
35+
android:padding="12dp"
36+
android:src="@drawable/icon_set" />
3437
</LinearLayout>
3538

3639

@@ -51,22 +54,36 @@
5154
android:layout_height="0dp"
5255
android:layout_weight="1.5" />
5356

54-
<TextView
57+
<RelativeLayout
5558
android:layout_width="match_parent"
56-
android:layout_height="wrap_content"
57-
android:gravity="center"
58-
android:paddingTop="10dp"
59-
android:paddingBottom="10dp"
60-
android:text="京东pt_key列表,点击复制"
61-
android:textColor="#FF5757"
62-
android:textSize="14sp"
59+
android:layout_height="40dp"
6360
android:background="#e5e5e5"
64-
android:textStyle="bold" />
61+
android:orientation="horizontal">
62+
63+
<TextView
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:layout_centerInParent="true"
67+
android:gravity="center"
68+
android:text="京东pt_key列表,点击复制"
69+
android:textColor="@color/purple_200"
70+
android:textSize="14sp"
71+
android:textStyle="bold" />
72+
73+
<ImageView
74+
android:id="@+id/main_clear"
75+
android:layout_width="45dp"
76+
android:layout_height="45dp"
77+
android:layout_alignParentEnd="true"
78+
android:padding="12dp"
79+
android:src="@drawable/icon_clear" />
80+
</RelativeLayout>
81+
6582

6683
<androidx.recyclerview.widget.RecyclerView
6784
android:id="@+id/main_recycler_view"
68-
android:scrollbars="vertical"
6985
android:layout_width="match_parent"
7086
android:layout_height="0dp"
71-
android:layout_weight="1" />
87+
android:layout_weight="1"
88+
android:scrollbars="vertical" />
7289
</LinearLayout>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
tools:context=".activity.SetActivity">
9+
10+
11+
<RelativeLayout
12+
android:layout_width="match_parent"
13+
android:layout_height="45dp"
14+
android:background="@color/purple_200"
15+
android:orientation="horizontal">
16+
17+
<ImageView
18+
android:id="@+id/set_back_image"
19+
android:layout_width="45dp"
20+
android:layout_height="45dp"
21+
android:padding="16dp"
22+
android:src="@drawable/icon_back"
23+
app:tint="#fff" />
24+
25+
<TextView
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:layout_centerInParent="true"
29+
android:text="设置"
30+
android:textColor="#fff"
31+
android:textSize="16sp" />
32+
33+
</RelativeLayout>
34+
35+
36+
<androidx.core.widget.NestedScrollView
37+
android:layout_width="match_parent"
38+
android:layout_height="match_parent"
39+
android:fillViewport="true">
40+
41+
<LinearLayout
42+
android:layout_width="match_parent"
43+
android:layout_height="match_parent"
44+
android:background="#eee"
45+
android:orientation="vertical">
46+
47+
48+
<LinearLayout
49+
android:id="@+id/set_clear_cookie_layout"
50+
android:layout_width="match_parent"
51+
android:layout_height="45dp"
52+
android:layout_marginTop="5dp"
53+
android:background="#fff"
54+
android:gravity="center"
55+
android:orientation="horizontal"
56+
android:paddingStart="12dp"
57+
android:paddingEnd="12dp">
58+
59+
<TextView
60+
android:layout_width="0dp"
61+
android:layout_height="wrap_content"
62+
android:layout_weight="1"
63+
android:text="清除浏览器Cookie"
64+
android:textColor="#333"
65+
android:textSize="14sp" />
66+
67+
<ImageView
68+
android:layout_width="25dp"
69+
android:layout_height="25dp"
70+
android:src="@drawable/icon_arrow_more" />
71+
72+
</LinearLayout>
73+
74+
75+
</LinearLayout>
76+
77+
78+
</androidx.core.widget.NestedScrollView>
79+
</LinearLayout>

app/src/main/res/values-night/themes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<style name="Theme.JDCookie" parent="Theme.MaterialComponents.DayNight.NoActionBar">
44
<!-- Primary brand color. -->
55
<item name="colorPrimary">@color/purple_200</item>
6-
<item name="colorPrimaryVariant">@color/purple_700</item>
6+
<item name="colorPrimaryVariant">@color/purple_200</item>
77
<item name="colorOnPrimary">@color/black</item>
88
<!-- Secondary brand color. -->
99
<item name="colorSecondary">@color/teal_200</item>

app/src/main/res/values/themes.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<!-- Base application theme. -->
33
<style name="Theme.JDCookie" parent="Theme.MaterialComponents.DayNight.NoActionBar">
44
<!-- Primary brand color. -->
5-
<item name="colorPrimary">@color/purple_500</item>
6-
<item name="colorPrimaryVariant">@color/purple_700</item>
5+
<item name="colorPrimary">@color/purple_200</item>
6+
<item name="colorPrimaryVariant">@color/purple_200</item>
77
<item name="colorOnPrimary">@color/white</item>
88
<!-- Secondary brand color. -->
99
<item name="colorSecondary">@color/teal_200</item>

0 commit comments

Comments
 (0)