PrivacyActivity.java
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package cn.banxe.bxe;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PrivacyActivity extends Activity {
private static final String PREFS_NAME = "privacy_prefs";
private static final String KEY_AGREED = "privacy_agreed";
private static final String USER_AGREEMENT_URL = "https://bxr.banxiaoer.net/apps/useragreement.html";
private static final String PRIVACY_POLICY_URL = "https://bxr.banxiaoer.net/apps/privacysettings.html";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 检查是否已同意隐私政策
if (isPrivacyAgreed()) {
startMainActivity();
return;
}
setContentView(createContentView());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
// 如果已同意隐私政策,将新Intent转发给MainActivity
if (isPrivacyAgreed()) {
forwardToMainActivity(intent);
}
}
private boolean isPrivacyAgreed() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
return prefs.getBoolean(KEY_AGREED, false);
}
private void savePrivacyAgreed() {
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
prefs.edit().putBoolean(KEY_AGREED, true).apply();
}
private View createContentView() {
// 根布局
LinearLayout rootLayout = new LinearLayout(this);
rootLayout.setOrientation(LinearLayout.VERTICAL);
rootLayout.setGravity(Gravity.CENTER_VERTICAL);
rootLayout.setPadding(dpToPx(32), dpToPx(48), dpToPx(32), dpToPx(48));
rootLayout.setBackgroundColor(0xFFFFFFFF);
// 内容容器
LinearLayout contentLayout = new LinearLayout(this);
contentLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams contentLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
rootLayout.addView(contentLayout, contentLayoutParams);
// 标题
TextView titleView = new TextView(this);
titleView.setText("用户协议与隐私政策");
titleView.setTextSize(22);
titleView.setTextColor(0xFF333333);
titleView.setTypeface(null, Typeface.BOLD);
titleView.setGravity(Gravity.CENTER);
titleView.setPadding(0, 0, 0, dpToPx(32));
contentLayout.addView(titleView);
// 内容(带可点击链接)
TextView contentView = new TextView(this);
contentView.setText(createClickableContent());
contentView.setMovementMethod(LinkMovementMethod.getInstance());
contentView.setHighlightColor(0x00000000);
contentView.setTextSize(17);
contentView.setTextColor(0xFF666666);
contentView.setLineSpacing(dpToPx(6), 1.0f);
contentView.setGravity(Gravity.START);
contentLayout.addView(contentView);
// 按钮容器
LinearLayout buttonLayout = new LinearLayout(this);
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
buttonLayout.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
buttonLayoutParams.topMargin = dpToPx(48);
contentLayout.addView(buttonLayout, buttonLayoutParams);
// 同意按钮
Button agreeButton = new Button(this);
agreeButton.setText("同意");
agreeButton.setTextColor(0xFFFFFFFF);
agreeButton.setBackgroundColor(0xFF4CAF50);
agreeButton.setTextSize(16);
agreeButton.setGravity(Gravity.CENTER);
agreeButton.setSingleLine(true);
agreeButton.setMinWidth(dpToPx(120));
agreeButton.setMinHeight(dpToPx(48));
int btnPaddingH = dpToPx(24);
int btnPaddingV = dpToPx(12);
agreeButton.setPadding(btnPaddingH, btnPaddingV, btnPaddingH, btnPaddingV);
agreeButton.setOnClickListener(v -> onAgreeClicked());
LinearLayout.LayoutParams agreeParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
agreeParams.setMargins(dpToPx(16), 0, dpToPx(16), 0);
buttonLayout.addView(agreeButton, agreeParams);
// 不同意按钮
Button disagreeButton = new Button(this);
disagreeButton.setText("不同意");
disagreeButton.setTextColor(0xFF666666);
disagreeButton.setBackgroundColor(0xFFEEEEEE);
disagreeButton.setTextSize(16);
disagreeButton.setGravity(Gravity.CENTER);
disagreeButton.setSingleLine(true);
disagreeButton.setMinWidth(dpToPx(120));
disagreeButton.setMinHeight(dpToPx(48));
disagreeButton.setPadding(btnPaddingH, btnPaddingV, btnPaddingH, btnPaddingV);
disagreeButton.setOnClickListener(v -> onDisagreeClicked());
LinearLayout.LayoutParams disagreeParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
disagreeParams.setMargins(dpToPx(16), 0, dpToPx(16), 0);
buttonLayout.addView(disagreeButton, disagreeParams);
return rootLayout;
}
private int dpToPx(int dp) {
float density = getResources().getDisplayMetrics().density;
return (int) (dp * density + 0.5f);
}
private SpannableString createClickableContent() {
String text = "请您仔细阅读《用户协议》和《隐私政策》,\n充分理解协议内容。\n\n" +
"我们将严格遵守相关法律法规,保护您的个人信息安全。\n\n" +
"点击\"同意\"即表示您已阅读并同意相关协议。";
SpannableString spannableString = new SpannableString(text);
// 用户协议点击
int userAgreementStart = text.indexOf("《用户协议》");
int userAgreementEnd = userAgreementStart + "《用户协议》".length();
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
openBrowser(USER_AGREEMENT_URL);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(0xFF4CAF50);
ds.setUnderlineText(true);
}
}, userAgreementStart, userAgreementEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// 隐私政策点击
int privacyPolicyStart = text.indexOf("《隐私政策》");
int privacyPolicyEnd = privacyPolicyStart + "《隐私政策》".length();
spannableString.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
openBrowser(PRIVACY_POLICY_URL);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(0xFF4CAF50);
ds.setUnderlineText(true);
}
}, privacyPolicyStart, privacyPolicyEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannableString;
}
private void openBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
private void onAgreeClicked() {
savePrivacyAgreed();
startMainActivity();
}
private void onDisagreeClicked() {
new AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("您需要同意隐私政策才能使用本应用,是否重新考虑?")
.setPositiveButton("重新考虑", (dialog, which) -> {
// 不做任何操作,保持当前界面
})
.setNegativeButton("仍不同意", (dialog, which) -> {
// 退出应用
finishAffinity();
})
.setCancelable(false)
.show();
}
private void startMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
copyIntentData(getIntent(), intent);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
private void forwardToMainActivity(Intent sourceIntent) {
Intent intent = new Intent(this, MainActivity.class);
copyIntentData(sourceIntent, intent);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
private void copyIntentData(Intent source, Intent dest) {
if (source == null) return;
// 复制Extras
if (source.getExtras() != null) {
dest.putExtras(source.getExtras());
}
// 复制Data URI(微信回调可能使用)
if (source.getData() != null) {
dest.setData(source.getData());
}
// 复制Action
if (source.getAction() != null) {
dest.setAction(source.getAction());
}
// 复制所有分类
if (source.getCategories() != null) {
for (String category : source.getCategories()) {
dest.addCategory(category);
}
}
// 复制Flags(排除某些会导致问题的flags)
int flags = source.getFlags();
flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
flags &= ~Intent.FLAG_ACTIVITY_CLEAR_TASK;
dest.setFlags(flags);
}
@Override
public void onBackPressed() {
// 禁止返回键
}
}