Skip to content

Commit

Permalink
feat: jobscheduler, notifications, settings activity
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytroKarataiev committed Aug 11, 2016
1 parent bb87c79 commit dd1e86c
Show file tree
Hide file tree
Showing 13 changed files with 563 additions and 14 deletions.
4 changes: 1 addition & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android {
buildToolsVersion "24.0.0"

defaultConfig {
applicationId "com.adkdevelopment.bbcreader"
applicationId "com.adkdevelopment.rssreader"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
Expand All @@ -48,7 +48,6 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

}

ext {
Expand All @@ -57,7 +56,6 @@ ext {
butterknifeVersion = "8.1.0"
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
Expand Down
35 changes: 32 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,54 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- To be able to restore JobService on reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:name=".App"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar">
<activity android:name=".ui.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- to be searchable by Google -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="www.adkdevelopment.com"
android:scheme="http" />
</intent-filter>
</activity>
<activity
android:name=".ui.DetailActivity"
android:label="@string/app_name"
android:parentActivityName=".ui.MainActivity" />
android:parentActivityName=".ui.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
<activity
android:name=".ui.settings.SettingsActivity"
android:label="@string/settings_title"
android:parentActivityName=".ui.MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.MainActivity" />
</activity>
<service
android:name=".data.services.FetchJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true" />

</application>

Expand Down
31 changes: 31 additions & 0 deletions app/src/main/java/com/adkdevelopment/rssreader/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@

package com.adkdevelopment.rssreader;

import android.annotation.TargetApi;
import android.app.Application;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.text.format.DateUtils;
import android.util.Log;

import com.adkdevelopment.rssreader.data.managers.ApiManager;
import com.adkdevelopment.rssreader.data.managers.DataManager;
import com.adkdevelopment.rssreader.data.managers.PreferenceManager;
import com.adkdevelopment.rssreader.data.services.FetchJobService;

import io.realm.Realm;
import io.realm.RealmConfiguration;
Expand All @@ -40,17 +48,21 @@
*/
public class App extends Application {

private static final String TAG = App.class.getSimpleName();

private static Context sContext;

private static DataManager sDataManager;
private static ApiManager sApiManager;
private static PreferenceManager sSharedPrefManager;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onCreate() {
super.onCreate();
sContext = getApplicationContext();
setupRealmDefaultInstance();
scheduleJob();
}

/**
Expand Down Expand Up @@ -117,4 +129,23 @@ public void clear() {
sSharedPrefManager.clear();
}

/**
* Schedules a Job to update news and send notifications.
*/
private void scheduleJob() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int JOB_ID = 123;
ComponentName serviceName = new ComponentName(this, FetchJobService.class);
JobInfo jobInfo = new JobInfo.Builder(JOB_ID, serviceName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(getSharedPrefManager().getSyncInterval() * DateUtils.MINUTE_IN_MILLIS)
.setPersisted(true)
.build();
JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
int result = scheduler.schedule(jobInfo);
if (result == JobScheduler.RESULT_SUCCESS) {
Log.v(TAG, "Job scheduled successfully!");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public interface Manager {
*/
interface PrefsManager extends Manager {
SharedPreferences getSharedPrefs();

boolean receiveNotifications();

long getLastNotification();

void setLastNotification();

int getSyncInterval();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.Context;
import android.content.SharedPreferences;

import com.adkdevelopment.rssreader.R;
import com.adkdevelopment.rssreader.data.contracts.Manager;

/**
Expand All @@ -39,7 +40,6 @@ public class PreferenceManager implements Manager.PrefsManager {
private static Context sContext;

public PreferenceManager() {

}

@Override
Expand All @@ -57,4 +57,50 @@ public void clear() {
public SharedPreferences getSharedPrefs() {
return sPref;
}

/**
* Returns true is user wants to receive notifications,
* false otherwise.
* @return boolean to send notifications.
*/
@Override
public boolean receiveNotifications() {
return getSharedPrefs()
.getBoolean(sContext.getString(R.string.sharedprefs_key_notifications), true);
}

/**
* Returns time in long of last issued notification.
* @return long time of last notification.
*/
@Override
public long getLastNotification() {
return getSharedPrefs()
.getLong(sContext.getString(R.string.sharedprefs_key_lastnotification),
System.currentTimeMillis());
}

/**
* Sets last notification date to current time.
*/
@Override
public void setLastNotification() {
getSharedPrefs()
.edit()
.putLong(sContext.getString(R.string.sharedprefs_key_lastnotification),
System.currentTimeMillis())
.apply();
}

/**
* Method to get SyncInterval from SharedPreferences
* @return interval in minutes
*/
@Override
public int getSyncInterval() {
String syncFrequency = getSharedPrefs()
.getString(sContext.getString(R.string.sharedprefs_key_syncfrequency), "7200");

return Integer.parseInt(syncFrequency);
}
}
Loading

0 comments on commit dd1e86c

Please sign in to comment.