PrivacyActivity.java 10.9 KB
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.drawable.GradientDrawable;
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.FrameLayout;
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() {
        // 外层根布局(浅灰色背景,垂直居中)
        FrameLayout rootLayout = new FrameLayout(this);
        rootLayout.setBackgroundColor(0xFFFFFFFF);

        // 卡片容器(白色圆角卡片)
        LinearLayout cardLayout = new LinearLayout(this);
        cardLayout.setOrientation(LinearLayout.VERTICAL);
        cardLayout.setPadding(dpToPx(28), dpToPx(36), dpToPx(28), dpToPx(36));
        GradientDrawable cardBg = new GradientDrawable();
        cardBg.setColor(0xFFFFFFFF);
        cardBg.setCornerRadius(dpToPx(16));
        cardLayout.setBackground(cardBg);

        FrameLayout.LayoutParams cardParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        int cardMarginH = dpToPx(24);
        int cardMarginV = dpToPx(80);
        cardParams.setMargins(cardMarginH, cardMarginV, cardMarginH, cardMarginV);
        cardParams.gravity = Gravity.CENTER_VERTICAL;
        rootLayout.addView(cardLayout, cardParams);

        // 标题
        TextView titleView = new TextView(this);
        titleView.setText("用户协议与隐私政策");
        titleView.setTextSize(23);
        titleView.setTextColor(0xFF333333);
        titleView.setTypeface(null, Typeface.BOLD);
        titleView.setGravity(Gravity.CENTER);
        titleView.setPadding(0, 0, 0, dpToPx(28));
        cardLayout.addView(titleView);

        // 内容(带可点击链接)
        TextView contentView = new TextView(this);
        contentView.setText(createClickableContent());
        contentView.setMovementMethod(LinkMovementMethod.getInstance());
        contentView.setHighlightColor(0x00000000);
        contentView.setTextSize(17);
        contentView.setTextColor(0xFF555555);
        contentView.setLineSpacing(dpToPx(6), 1.0f);
        contentView.setGravity(Gravity.START);
        cardLayout.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(32);
        cardLayout.addView(buttonLayout, buttonLayoutParams);

        int btnHeight = dpToPx(44);
        int btnRadius = dpToPx(22);

        // 同意按钮(蓝紫色填充,大圆角)
        Button agreeButton = new Button(this);
        agreeButton.setText("同意");
        agreeButton.setTextColor(0xFFFFFFFF);
        agreeButton.setTextSize(16);
        agreeButton.setTypeface(null, Typeface.BOLD);
        agreeButton.setGravity(Gravity.CENTER);
        agreeButton.setSingleLine(true);
        agreeButton.setPadding(0, 0, 0, 0);
        GradientDrawable agreeBg = new GradientDrawable();
        agreeBg.setColor(0xFF7691FA);
        agreeBg.setCornerRadius(btnRadius);
        agreeButton.setBackground(agreeBg);
        agreeButton.setOnClickListener(v -> onAgreeClicked());
        LinearLayout.LayoutParams agreeParams = new LinearLayout.LayoutParams(
                0,
                btnHeight,
                1.0f);
        agreeParams.setMargins(0, 0, dpToPx(8), 0);
        buttonLayout.addView(agreeButton, agreeParams);

        // 不同意按钮(灰色描边,大圆角)
        Button disagreeButton = new Button(this);
        disagreeButton.setText("不同意");
        disagreeButton.setTextColor(0xFF666666);
        disagreeButton.setTextSize(16);
        disagreeButton.setGravity(Gravity.CENTER);
        disagreeButton.setSingleLine(true);
        disagreeButton.setPadding(0, 0, 0, 0);
        GradientDrawable disagreeBg = new GradientDrawable();
        disagreeBg.setColor(0xFFF5F6FA);
        disagreeBg.setCornerRadius(btnRadius);
        disagreeButton.setBackground(disagreeBg);
        disagreeButton.setOnClickListener(v -> onDisagreeClicked());
        LinearLayout.LayoutParams disagreeParams = new LinearLayout.LayoutParams(
                0,
                btnHeight,
                1.0f);
        disagreeParams.setMargins(dpToPx(8), 0, 0, 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" +
                "点击“同意”即表示您已阅读并同意相关协议。";
        
        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(0xFF7691FA);
                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(0xFF7691FA);
                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() {
        // 触发 Application 中的延迟初始化
        MyApplication.onPrivacyAgreed();
        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() {
        // 禁止返回键
    }
}