Skip to content

Commit

Permalink
更新代码,fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
leon committed Feb 11, 2016
1 parent 6199907 commit efa56a0
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 90 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.codeboy.qianghongbao"
minSdkVersion 16
targetSdkVersion 21
versionCode 303
versionName "3.0.3"
versionCode 307
versionName "3.0.7"
}

lintOptions {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/codeboy/qianghongbao/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ public boolean isEnableNotificationService() {
return preferences.getBoolean(KEY_NOTIFICATION_SERVICE_ENABLE, false);
}

public void setNotificationServiceEnable(boolean enable) {
preferences.edit().putBoolean(KEY_NOTIFICATION_SERVICE_ENABLE, enable).apply();
}

/** 是否开启声音*/
public boolean isNotifySound() {
return preferences.getBoolean(KEY_NOTIFY_SOUND, true);
Expand Down
68 changes: 42 additions & 26 deletions app/src/main/java/com/codeboy/qianghongbao/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private void openNotificationServiceSettings() {
public static class MainFragment extends BaseSettingsFragment {

private SwitchPreference notificationPref;
private boolean targetNotificationValue;
private boolean notificationChangeByUser = true;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -323,21 +323,26 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
return true;
}
});
if(!UmengConfig.isEnableWechat(getActivity())) {
wechatPref.setEnabled(false);
wechatPref.setTitle("暂时不能使用");
}

notificationPref = (SwitchPreference) findPreference(Config.KEY_NOTIFICATION_SERVICE_ENABLE);
notificationPref = (SwitchPreference) findPreference("KEY_NOTIFICATION_SERVICE_TEMP_ENABLE");
notificationPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
Toast.makeText(getActivity(), "该功能只支持安卓4.3以上的系统", Toast.LENGTH_SHORT).show();
return false;
}
targetNotificationValue = (Boolean)newValue;
if((Boolean) newValue && !QiangHongBaoService.isNotificationServiceRunning()) {

if(!notificationChangeByUser) {
notificationChangeByUser = true;
return true;
}

boolean enalbe = (boolean) newValue;

Config.getConfig(getActivity()).setNotificationServiceEnable(enalbe);

if(enalbe && !QiangHongBaoService.isNotificationServiceRunning()) {
((MainActivity)getActivity()).openNotificationServiceSettings();
return false;
}
Expand All @@ -346,23 +351,29 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});

findPreference("KEY_FOLLOW_ME").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
((MainActivity)getActivity()).showQrDialog();
QHBApplication.eventStatistics(getActivity(), "about_author");
return true;
}
});
Preference preference = findPreference("KEY_FOLLOW_ME");
if(preference != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
((MainActivity) getActivity()).showQrDialog();
QHBApplication.eventStatistics(getActivity(), "about_author");
return true;
}
});
}

findPreference("KEY_DONATE_ME").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
((MainActivity)getActivity()).showDonateDialog();
QHBApplication.eventStatistics(getActivity(), "donate");
return true;
}
});
preference = findPreference("KEY_DONATE_ME");
if(preference != null) {
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
((MainActivity) getActivity()).showDonateDialog();
QHBApplication.eventStatistics(getActivity(), "donate");
return true;
}
});
}

findPreference("WECHAT_SETTINGS").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
Expand All @@ -379,17 +390,22 @@ public boolean onPreferenceClick(Preference preference) {
return true;
}
});

}

/** 更新快速读取通知的设置*/
public void updateNotifyPreference() {
if(notificationPref == null) {
return;
}
if(targetNotificationValue && !notificationPref.isChecked() && QiangHongBaoService.isNotificationServiceRunning()) {
boolean running = QiangHongBaoService.isNotificationServiceRunning();
boolean enable = Config.getConfig(getActivity()).isEnableNotificationService();
if( enable && running && !notificationPref.isChecked()) {
QHBApplication.eventStatistics(getActivity(), "notify_service", String.valueOf(true));
notificationChangeByUser = false;
notificationPref.setChecked(true);
} else if(notificationPref.isChecked() && !QiangHongBaoService.isNotificationServiceRunning()) {
} else if((!enable || !running) && notificationPref.isChecked()) {
notificationChangeByUser = false;
notificationPref.setChecked(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcelable;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
Expand Down Expand Up @@ -95,20 +94,9 @@ public void onStopJob() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onNotificationPosted(IStatusBarNotification sbn) {
Notification notification = sbn.getNotification();
String ticker = String.valueOf(sbn.getNotification().tickerText);
if(!ticker.contains(HONGBAO_TEXT_KEY)) {
return;
}
try {
PendingIntent pendingIntent = notification.contentIntent;
if(!NotifyHelper.isLockScreen(getContext())) {
pendingIntent.send();
}
NotifyHelper.checkAndPlayNotify(getContext(), getConfig(), pendingIntent, true);
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
Notification nf = sbn.getNotification();
String text = String.valueOf(sbn.getNotification().tickerText);
notificationEvent(text, nf);
}

@Override
Expand All @@ -131,13 +119,9 @@ public void onReceiveJob(AccessibilityEvent event) {
}
List<CharSequence> texts = event.getText();
if(!texts.isEmpty()) {
for(CharSequence t : texts) {
String text = String.valueOf(t);
if(text.contains(HONGBAO_TEXT_KEY)) {
openNotify(event);
break;
}
}
String text = String.valueOf(texts.get(0));
Notification nf = (Notification) event.getParcelableData();
notificationEvent(text, nf);
}
} else if(eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
openHongBao(event);
Expand Down Expand Up @@ -203,25 +187,35 @@ private boolean isMemberChatUi(AccessibilityNodeInfo nodeInfo) {
return false;
}

/** 通知栏事件*/
private void notificationEvent(String ticker, Notification nf) {
String text = ticker;
int index = text.indexOf(":");
if(index != -1) {
text = text.substring(index + 1);
}
text = text.trim();
if(text.contains(HONGBAO_TEXT_KEY)) { //红包消息
newHongBaoNotification(nf);
}
}

/** 打开通知栏消息*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void openNotify(AccessibilityEvent event) {
Parcelable data = event.getParcelableData();
if(data == null || !(data instanceof Notification)) {
return;
private void newHongBaoNotification(Notification notification) {
isReceivingHongbao = true;
//以下是精华,将微信的通知栏消息打开
PendingIntent pendingIntent = notification.contentIntent;
boolean lock = NotifyHelper.isLockScreen(getContext());

if(!lock) {
NotifyHelper.send(pendingIntent);
} else {
NotifyHelper.showNotify(getContext(), String.valueOf(notification.tickerText), pendingIntent);
}

isReceivingHongbao = true;
try {
//以下是精华,将微信的通知栏消息打开
Notification notification = (Notification) data;
PendingIntent pendingIntent = notification.contentIntent;
if(!NotifyHelper.isLockScreen(getContext())) {
pendingIntent.send();
}
NotifyHelper.checkAndPlayNotify(getContext(), getConfig(), pendingIntent, true);
} catch (Exception e) {
e.printStackTrace();
if(lock || getConfig().getWechatMode() != Config.WX_MODE_0) {
NotifyHelper.playEffect(getContext(), getConfig());
}
}

Expand Down Expand Up @@ -273,7 +267,6 @@ private void handleLuckyMoneyReceive() {

if(buttonId != null) {
targetNode = AccessibilityHelper.findNodeInfosById(nodeInfo, buttonId);

}

if(targetNode == null) {
Expand Down
34 changes: 16 additions & 18 deletions app/src/main/java/com/codeboy/qianghongbao/util/NotifyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class NotifyHelper {
private static PowerManager sPowerManager;

/** 播放声音*/
public static void playSound(Context context) {
public static void sound(Context context) {
try {
MediaPlayer player = MediaPlayer.create(context,
Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
Expand All @@ -46,7 +46,7 @@ public static void vibrator(Context context) {
}

/** 是否为夜间*/
public static boolean isNight() {
public static boolean isNightTime() {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR_OF_DAY);
if(hour >= 23 || hour < 7) {
Expand Down Expand Up @@ -85,34 +85,32 @@ public static boolean isScreenOn(Context context) {
}
}

/** 检查并播放通知*/
public static void checkAndPlayNotify(Context context, Config config, PendingIntent pendingIntent, boolean fromWechat) {
/** 播放效果、声音与震动*/
public static void playEffect(Context context, Config config) {
//夜间模式,不处理
if(NotifyHelper.isNight() && config.isNotifyNight()) {
if(NotifyHelper.isNightTime() && config.isNotifyNight()) {
return;
}

boolean lock = isLockScreen(context);
if(!lock && fromWechat && config.getWechatMode() == Config.WX_MODE_0) {
return;
} else if(!lock && !fromWechat) { //非锁屏下
return;
}

if(lock) {
showNotify(context, pendingIntent);
}

if(config.isNotifySound()) {
playSound(context);
sound(context);
}
if(config.isNotifyVibrate()) {
vibrator(context);
}
}

/** 显示通知*/
public static void showNotify(Context context, PendingIntent pendingIntent) {
public static void showNotify(Context context, String title, PendingIntent pendingIntent) {

}

/** 执行PendingIntent事件*/
public static void send(PendingIntent pendingIntent) {
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.codeboy.qianghongbao.widget;

import android.content.Context;
import android.os.Build;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;

/**
* <p>Created 16/2/7 下午7:38.</p>
* <p><a href="mailto:730395591@qq.com">Email:730395591@qq.com</a></p>
* <p><a href="http://www.happycodeboy.com">LeonLee Blog</a></p>
*
* @author LeonLee
*/
public class CustomSwitchPreference extends SwitchPreference {

/**
* Construct a new SwitchPreference with default style options.
*
* @param context The Context that will style this preference
*/
public CustomSwitchPreference(Context context) {
super(context, null);
}

/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
*/
public CustomSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

/**
* Construct a new SwitchPreference with the given style options.
*
* @param context The Context that will style this preference
* @param attrs Style attributes that differ from the default
* @param defStyle Theme attribute defining the default style options
*/
public CustomSwitchPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onBindView(View view) {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
// Clean listener before invoke SwitchPreference.onBindView
ViewGroup viewGroup = (ViewGroup) view;
clearListenerInViewGroup(viewGroup);
}
super.onBindView(view);
}

/**
* Clear listener in Switch for specify ViewGroup.
*
* @param viewGroup The ViewGroup that will need to clear the listener.
*/
private void clearListenerInViewGroup(ViewGroup viewGroup) {
if (null == viewGroup) {
return;
}

int count = viewGroup.getChildCount();
for (int n = 0; n < count; ++n) {
View childView = viewGroup.getChildAt(n);
if (childView instanceof Switch) {
final Switch switchView = (Switch) childView;
switchView.setOnCheckedChangeListener(null);
return;
} else if (childView instanceof ViewGroup) {
ViewGroup childGroup = (ViewGroup) childView;
clearListenerInViewGroup(childGroup);
}
}
}

}
Loading

0 comments on commit efa56a0

Please sign in to comment.