NotificationsSettingActivity.java 6.2 KB
Newer Older
ydl committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
package com.yidianling.user.mine;

import android.annotation.TargetApi;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.ydl.ydlcommon.base.BaseActivity;
17
import com.ydl.ydlcommon.bean.StatusBarOptions;
ydl committed
18 19 20 21 22
import com.ydl.ydlcommon.data.PlatformDataManager;
import com.ydl.ydlcommon.modular.ModularServiceManager;
import com.yidianling.im.api.service.IImService;
import com.yidianling.user.R;

23 24
import org.jetbrains.annotations.NotNull;

ydl committed
25 26 27 28 29 30 31 32
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * Created by Wi1ls on 2016/10/28;
 */
33
@Route(path = "/user/notifysetting")
ydl committed
34 35 36 37 38 39 40 41 42 43 44
public class NotificationsSettingActivity extends BaseActivity {
    TextView tv_isopen;
    ToggleButton tb_voice;
    ToggleButton tb_shake;


    @Override
    protected int layoutResId() {
        return R.layout.user_mine_activity_notifications_setting;
    }

45 46 47 48 49 50
    @NotNull
    @Override
    public StatusBarOptions getStatusViewOptions() {
        return new StatusBarOptions(true,true);
    }

ydl committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    @Override
    protected void initDataAndEvent() {
        tv_isopen = findViewById(R.id.tv_isopen);
        tb_voice = findViewById(R.id.tb_voice);
        tb_shake = findViewById(R.id.tb_shake);
        init();
    }


    void init() {
        a();
        tb_voice.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                //设置声音开关
//                LogoutHelper.getInstance().saveVoice(isChecked);
                PlatformDataManager.INSTANCE.getLocal().putVoice(isChecked);
                try {
                    ModularServiceManager.INSTANCE.provide(IImService.class).imInSetRing(isChecked);
                } catch (Exception e) {}
ydl committed
72
//                JPushManager.INSTANCE.updateConfig(getApplicationContext());
ydl committed
73 74 75 76 77 78 79 80 81 82 83 84 85
            }
        });


        tb_shake.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //设置震动开关
//                LogoutHelper.getInstance().saveShake(isChecked);
                PlatformDataManager.INSTANCE.getLocal().putShake(isChecked);
                try {
                    ModularServiceManager.INSTANCE.provide(IImService.class).imInSetVibrate(isChecked);
                } catch (Exception e) {}
ydl committed
86
//                JPushManager.INSTANCE.updateConfig(getApplicationContext());
ydl committed
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
            }
        });

        tv_isopen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAppDetailSettingIntent(NotificationsSettingActivity.this);
            }
        });
    }


    private void a() {
        if (isNotificationEnabled(this)) {
            tv_isopen.setText("已开启");
            tb_voice.setClickable(true);
            tb_shake.setClickable(true);
            if (PlatformDataManager.INSTANCE.getLocal().isVoice()) {
                tb_voice.setChecked(true);
            } else {
                tb_voice.setChecked(false);
            }
            if (PlatformDataManager.INSTANCE.getLocal().isShake()) {
                tb_shake.setChecked(true);
            } else {
                tb_shake.setChecked(false);
            }
        } else {
            tv_isopen.setText("未开启");
            tb_voice.setChecked(false);
            tb_voice.setClickable(false);

            tb_shake.setChecked(false);
            tb_shake.setClickable(false);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        a();
    }

    private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
    private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";

    @TargetApi(Build.VERSION_CODES.KITKAT)
    private boolean isNotificationEnabled(Context context) {

        AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);

        ApplicationInfo appInfo = context.getApplicationInfo();

        String pkg = context.getApplicationContext().getPackageName();

        int uid = appInfo.uid;

        Class appOpsClass = null; /* Context.APP_OPS_MANAGER */

        try {

            appOpsClass = Class.forName(AppOpsManager.class.getName());

            Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);

            Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
            int value = (int) opPostNotificationValue.get(Integer.class);

            return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    private void getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(localIntent);
    }

}