Skip to content

Commit

Permalink
[ADD] 增加设置,清除浏览器的cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuSky committed Sep 1, 2021
1 parent a45f461 commit 43ddf79
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 67 deletions.
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.jdcookie">

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

<application
android:allowBackup="true"
Expand All @@ -11,7 +11,10 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.JDCookie">
<activity android:name=".MainActivity">
<activity android:name=".activity.SetActivity" />
<activity
android:name=".activity.MainActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.app.jdcookie;
package com.app.jdcookie.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -9,15 +8,13 @@
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
Expand All @@ -27,25 +24,9 @@
import android.widget.ProgressBar;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import okhttp3.Cookie;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import com.app.jdcookie.CookieListener;
import com.app.jdcookie.MyAdapter;
import com.app.jdcookie.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

findViewById(R.id.main_back).setOnClickListener(this);
findViewById(R.id.main_clear).setOnClickListener(this);
findViewById(R.id.main_set).setOnClickListener(this);
webBridgeProgressBar = findViewById(R.id.main_progress_bar);

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

webSetting.setBlockNetworkImage(false);
// webSetting.setUserAgentString(userAgent);
// if (NetWorkUtils.isNetConnected(App.getAppContext())) {
//缓存模式
webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
// } else {
// //缓存模式
// webSetting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
// }

webSetting.setDatabaseEnabled(true);
webSetting.setDomStorageEnabled(true);
webSetting.setAppCacheMaxSize(1024 * 1024 * 8);
Expand Down Expand Up @@ -183,7 +158,6 @@ public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceReque
//截取到";"前的 pt_key
pt_key = pt_key.substring(0, pt_key.indexOf(";", 1) + 1);


cookieListener.onCookie(cookie, pt_key + pt_pin);
}
} catch (Exception e) {
Expand All @@ -205,38 +179,38 @@ public MyWebChromeClient(ProgressBar webBridgeProgressBar) {
public void onProgressChanged(WebView view, int newProgress) {
if (null != webBridgeProgressBar) {
webBridgeProgressBar.setProgress(newProgress);
if (newProgress == 100) {
}
}
super.onProgressChanged(view, newProgress);
}

@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (!view.getTitle().startsWith("http")) {
// if (webBridgeNormalTitleBar != null) {
// webBridgeNormalTitleBar.setTitleText(view.getTitle());
// }
}
}

}

@Override
public void onBackPressed() {
//返回上一页面
if (webBridgeWebView.canGoBack()) {
webBridgeWebView.goBack();
} else {
onBackPressed();
}
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.main_back) {
//返回上一页面
if (webBridgeWebView.canGoBack()) {
webBridgeWebView.goBack();
} else {
onBackPressed();
}
onBackPressed();
} else if (v.getId() == R.id.main_clear) {
if (adapter != null) {
adapter.getData().clear();
adapter.notifyDataSetChanged();
}
} else if (v.getId() == R.id.main_set) {
startActivity(new Intent(this, SetActivity.class));
}
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/app/jdcookie/activity/SetActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.app.jdcookie.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.widget.Toast;

import com.app.jdcookie.R;

public class SetActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set);


findViewById(R.id.set_back_image).setOnClickListener(this);
findViewById(R.id.set_clear_cookie_layout).setOnClickListener(this);
}

@Override
public void onClick(View v) {
if (v.getId() == R.id.set_back_image) {

onBackPressed();
} else if (v.getId() == R.id.set_clear_cookie_layout) {
//清除浏览器的cookie
CookieManager instance = CookieManager.getInstance();
instance.removeAllCookies(value -> Toast.makeText(SetActivity.this, "清除" + (value ? "成功" : "失败"), Toast.LENGTH_SHORT).show());
instance.flush();
}
}
}
Binary file added app/src/main/res/drawable/icon_arrow_more.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/icon_set.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 34 additions & 17 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,35 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
tools:context=".activity.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/purple_200"
android:orientation="horizontal">

<ImageView
android:id="@+id/main_back"
android:layout_width="45dp"
android:layout_height="45dp"
android:padding="10dp"
android:src="@drawable/icon_back" />
android:src="@drawable/icon_back"
app:tint="#fff" />

<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />

<ImageView
android:id="@+id/main_clear"
android:id="@+id/main_set"
android:layout_width="45dp"
android:layout_height="45dp"
android:padding="10dp"
android:src="@drawable/icon_clear" />

android:layout_alignParentEnd="true"
app:tint="#fff"
android:padding="12dp"
android:src="@drawable/icon_set" />
</LinearLayout>


Expand All @@ -51,22 +54,36 @@
android:layout_height="0dp"
android:layout_weight="1.5" />

<TextView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="京东pt_key列表,点击复制"
android:textColor="#FF5757"
android:textSize="14sp"
android:layout_height="40dp"
android:background="#e5e5e5"
android:textStyle="bold" />
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="京东pt_key列表,点击复制"
android:textColor="@color/purple_200"
android:textSize="14sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/main_clear"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
android:padding="12dp"
android:src="@drawable/icon_clear" />
</RelativeLayout>


<androidx.recyclerview.widget.RecyclerView
android:id="@+id/main_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
android:layout_weight="1"
android:scrollbars="vertical" />
</LinearLayout>
79 changes: 79 additions & 0 deletions app/src/main/res/layout/activity_set.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.SetActivity">


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/purple_200"
android:orientation="horizontal">

<ImageView
android:id="@+id/set_back_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:padding="16dp"
android:src="@drawable/icon_back"
app:tint="#fff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="设置"
android:textColor="#fff"
android:textSize="16sp" />

</RelativeLayout>


<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical">


<LinearLayout
android:id="@+id/set_clear_cookie_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:orientation="horizontal"
android:paddingStart="12dp"
android:paddingEnd="12dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="清除浏览器Cookie"
android:textColor="#333"
android:textSize="14sp" />

<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/icon_arrow_more" />

</LinearLayout>


</LinearLayout>


</androidx.core.widget.NestedScrollView>
</LinearLayout>
2 changes: 1 addition & 1 deletion app/src/main/res/values-night/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<style name="Theme.JDCookie" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorPrimaryVariant">@color/purple_200</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<!-- Base application theme. -->
<style name="Theme.JDCookie" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_200</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
Expand Down

0 comments on commit 43ddf79

Please sign in to comment.