Skip to content

Commit

Permalink
add notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Nov 10, 2016
1 parent 374c582 commit d2b4f78
Show file tree
Hide file tree
Showing 36 changed files with 318 additions and 31 deletions.
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Expand Down Expand Up @@ -67,6 +66,8 @@
android:name=".activity.UserListActivity"
android:theme="@style/AppTheme" />
<activity android:name=".activity.DirectMessageActivity"/>

<service android:name=".common.service.NotificationService" />
</application>

</manifest>
Binary file added app/src/main/ic_logo_no_padding-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions app/src/main/java/moe/tlaster/openween/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package moe.tlaster.openween;

import android.app.Application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
Expand All @@ -12,6 +15,10 @@
import com.mikepenz.materialdrawer.util.DrawerImageLoader;
import com.mikepenz.materialdrawer.util.DrawerUIUtils;

import moe.tlaster.openween.common.service.NotificationService;

import static android.app.job.JobInfo.NETWORK_TYPE_ANY;

/**
* Created by Tlaster on 2016/9/10.
*/
Expand Down
72 changes: 69 additions & 3 deletions app/src/main/java/moe/tlaster/openween/activity/BaseActivity.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package moe.tlaster.openween.activity;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.transition.Explode;
Expand All @@ -17,9 +24,14 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.Objects;

import moe.tlaster.openween.R;
import moe.tlaster.openween.common.service.NotificationService;
import moe.tlaster.openween.core.api.Entity;

import static android.app.job.JobInfo.NETWORK_TYPE_ANY;

//import icepick.Icepick;

/**
Expand All @@ -28,21 +40,26 @@

public abstract class BaseActivity extends AppCompatActivity {
private String mShouldRestart = "ShouldRestart";
private int INTERVAL = 30 * 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Icepick.restoreInstanceState(this, savedInstanceState);
if (savedInstanceState != null && savedInstanceState.getBoolean(mShouldRestart, false)) {
//It's better to restart the app than restore the state
Intent intent = new Intent(this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
goSplash();
finish();
return;
}
checkForUpdates();
}

protected void goSplash() {
Intent intent = new Intent(this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Expand All @@ -58,11 +75,13 @@ public void onDestroy() {
public void onResume() {
super.onResume();
checkForCrashes();
stopNotification();
}
@Override
public void onPause() {
super.onPause();
unregisterManagers();
startNotification();
}
private void checkForCrashes() {
CrashManager.register(this);
Expand All @@ -75,4 +94,51 @@ private void unregisterManagers() {
private void checkForUpdates() {
UpdateManager.register(this);
}


public void startNotification() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 8975, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, getInterval(), pendingIntent);
}

private int getInterval() {
String[] choices = getResources().getStringArray(R.array.notification_interval_list);
String invertChoice = PreferenceManager.getDefaultSharedPreferences(this).getString(getString(R.string.notification_interval_key), getString(R.string.notification_interval_default));
int interval = 60 * 1000;
if (Objects.equals(invertChoice, choices[0])) {
interval = 60 * 1000;
} else if (Objects.equals(invertChoice, choices[1])) {
interval = 3 * 60 * 1000;
} else if (Objects.equals(invertChoice, choices[2])) {
interval = 5 * 60 * 1000;
} else if (Objects.equals(invertChoice, choices[3])) {
interval = 10 * 60 * 1000;
} else if (Objects.equals(invertChoice, choices[4])) {
interval = 30 * 60 * 1000;
}
return interval;
}

public void stopNotification() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 8975, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.cancel(pendingIntent);
}

/*
protected void startNotification() {
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo.Builder builder = new JobInfo.Builder(8975, new ComponentName(getPackageName(), NotificationService.class.getName()))
.setPeriodic(getInterval())
.setRequiredNetworkType(NETWORK_TYPE_ANY);
jobScheduler.schedule(builder.build());
}
protected void cancelAllJobs() {
((JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE)).cancel(8975);
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ public void onPageFinished(WebView view, String url) {
dialog.setCancelable(true);
}

private void goSplash() {
startActivity(new Intent(LoginActivity.this, SplashActivity.class));
finish();
}

@OnClick(R.id.login_what)
public void what(){
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!getIntent().hasExtra("user")) {
goSplash();
return;
}
UserModel user = getIntent().getExtras().getParcelable("user");
//setupWindowAnimations();
ButterKnife.bind(this);

Timeline timeline = new Timeline();

PrimaryDrawerItem drawerItemHome;

Drawer drawer = new DrawerBuilder()
.withActivity(this)
.addDrawerItems(
Expand All @@ -75,7 +77,6 @@ protected void onCreate(Bundle savedInstanceState) {
pageAdapter.add(new DirectMessage());
mPivot.setAdapter(pageAdapter);
mPivot.setOffscreenPageLimit(3);
UserModel user = getIntent().getExtras().getParcelable("user");
mPivot.setProfileImage(user.getAvatarLarge());
mPivot.setProfileImageOnClickListener(view -> WeiboCardHelper.goUserActivity(user.getScreenName(), MainActivity.this));
AccountHeaderBuilder accountHeaderBuilder = new AccountHeaderBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;

import net.hockeyapp.android.FeedbackManager;

import moe.tlaster.openween.App;
import moe.tlaster.openween.R;
import moe.tlaster.openween.common.StaticResource;
Expand Down Expand Up @@ -183,7 +185,24 @@ public void onBuildHeaders(List<Header> target) {
*/
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName);
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| NotificationPreferenceFragment.class.getName().equals(fragmentName);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
setHasOptionsMenu(false);
bindPreferenceSummaryToValue(findPreference(getString(R.string.enable_notification_name)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.notification_interval_key)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.is_comment_notify_name)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.is_mention_notify_name)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.is_follower_notify_name)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.is_message_notify_name)));
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
Expand All @@ -192,8 +211,12 @@ public static class GeneralPreferenceFragment extends PreferenceFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
setHasOptionsMenu(false);
bindPreferenceSummaryToValue(findPreference(getString(R.string.enable_animate_key)));
findPreference(getString(R.string.feedback_key)).setOnPreferenceClickListener(preference -> {
FeedbackManager.showFeedbackActivity(getActivity());
return true;
});
findPreference(getString(R.string.download_emotion_key)).setOnPreferenceClickListener(preference -> {
MaterialDialog[] dialog = {new MaterialDialog.Builder(getActivity())
.title("正在下载表情")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package moe.tlaster.openween.activity;

import android.app.ActivityOptions;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -34,6 +38,8 @@
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;

import net.hockeyapp.android.metrics.MetricsManager;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -50,6 +56,7 @@
import moe.tlaster.openween.common.helpers.DeviceHelper;
import moe.tlaster.openween.common.helpers.JsonCallback;
import moe.tlaster.openween.common.helpers.SettingHelper;
import moe.tlaster.openween.common.service.NotificationService;
import moe.tlaster.openween.core.api.Entity;
import moe.tlaster.openween.core.api.user.Account;
import moe.tlaster.openween.core.api.user.User;
Expand All @@ -58,6 +65,7 @@
import moe.tlaster.openween.core.model.user.UserModel;
import okhttp3.Call;

import static android.app.job.JobInfo.NETWORK_TYPE_ANY;
import static com.transitionseverywhere.TransitionSet.ORDERING_TOGETHER;

public class SplashActivity extends BaseActivity {
Expand All @@ -71,6 +79,7 @@ public class SplashActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MetricsManager.register(getApplication());
boolean enableAnimate = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(getString(R.string.enable_animate_key), true);
if (enableAnimate) {
setContentView(R.layout.activity_splash);
Expand Down
Loading

0 comments on commit d2b4f78

Please sign in to comment.