Skip to content

Commit

Permalink
support older devices than MNC
Browse files Browse the repository at this point in the history
  • Loading branch information
commonsguy committed Jun 13, 2015
1 parent 72d39f5 commit cda9354
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
24 changes: 17 additions & 7 deletions JobScheduler/DeepBackground/build.gradle
Expand Up @@ -3,7 +3,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0-beta1'
classpath 'com.android.tools.build:gradle:1.3.0-beta2'
}
}

Expand All @@ -14,14 +14,24 @@ dependencies {
}

android {
// compileSdkVersion 22
compileSdkVersion 'android-MNC'
buildToolsVersion "23.0.0 rc1"
buildToolsVersion "23.0.0 rc2"

defaultConfig {
minSdkVersion 'MNC'
// minSdkVersion 15
// targetSdkVersion 22
targetSdkVersion 'MNC'
minSdkVersion 15
targetSdkVersion 15
}

// based on http://stackoverflow.com/a/27372806/115145

applicationVariants.all { variant ->
variant.outputs.each { output ->
output.processManifest.doLast {
def manifestOutFile = output.processManifest.manifestOutputFile
def newFileContents = manifestOutFile.getText('UTF-8').replace("MNC", "15")
manifestOutFile.write(newFileContents, 'UTF-8')
}
}
}
}

Expand Up @@ -23,6 +23,7 @@
import android.os.SystemClock;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;
import java.lang.reflect.Field;

public class PollReceiver extends WakefulBroadcastReceiver {
static final String EXTRA_PERIOD="period";
Expand All @@ -46,14 +47,24 @@ public void onReceive(Context ctxt, Intent i) {
}
}

@TargetApi(Build.VERSION_CODES.KITKAT)
@TargetApi(Build.VERSION_CODES.MNC)
static void scheduleExactAlarm(Context ctxt, AlarmManager alarms,
long period, boolean isDownload) {
Intent i=buildBaseIntent(ctxt)
.putExtra(EXTRA_PERIOD, period)
.putExtra(EXTRA_IS_DOWNLOAD, isDownload);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

Log.e("PollReceiver", String.valueOf(Build.VERSION.SDK_INT));
Log.e("PollReceiver", String.valueOf(Build.VERSION_CODES.LOLLIPOP_MR1));
// Log.e("PollReceiver", String.valueOf(Build.VERSION.PREVIEW_SDK_INT));
Log.e("PollReceiver", String.valueOf(putMarzipanInYourPiePlateBingo()));

if (putMarzipanInYourPiePlateBingo()) {
Log.e("PollReceiver", "allow while idle");
alarms.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + period, pi);
}
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT) {
alarms.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + period, pi);
Expand All @@ -70,7 +81,7 @@ static void scheduleInexactAlarm(Context ctxt, AlarmManager alarms,
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0);

alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+period, period, pi);
SystemClock.elapsedRealtime() + period, period, pi);
}

static void cancelAlarm(Context ctxt, AlarmManager alarms) {
Expand All @@ -89,4 +100,26 @@ static boolean doesPendingIntentExist(Context ctxt) {
.getBroadcast(ctxt, 0, buildBaseIntent(ctxt),
PendingIntent.FLAG_NO_CREATE)!=null);
}

static private boolean putMarzipanInYourPiePlateBingo() {
boolean result=false;

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP_MR1) {
try {
Field f=Build.VERSION.class.getField("PREVIEW_SDK_INT");

if (f.getInt(null)>0) {
result=true;
}
}
catch (NoSuchFieldException e) {
// no problem, must really be API 22
}
catch (IllegalAccessException e) {
// ummm... this shouldn't happen
}
}

return(result);
}
}
34 changes: 10 additions & 24 deletions JobScheduler/DeepBackground/src/main/res/layout/main.xml
Expand Up @@ -8,73 +8,59 @@
android:useDefaultMargins="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:text="@string/type_label"/>

<Spinner
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"/>
android:layout_row="0"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:text="@string/period_label"/>

<Spinner
android:id="@+id/period"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"/>
android:layout_row="1"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:text="@string/download_label"/>

<Switch
android:id="@+id/download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"/>
android:layout_row="2"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:text="@string/scheduled_label"/>

<Switch
android:id="@+id/scheduled"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"/>
android:layout_row="3"
android:layout_weight="1"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="4"
android:text="@string/crash_freq"/>

<SeekBar
android:id="@+id/crash_freq"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="4"
android:layout_weight="1"
android:layout_marginRight="16dp"
android:max="25"/>
</GridLayout>
22 changes: 11 additions & 11 deletions JobScheduler/DeepBackground/src/main/res/values/arrays.xml
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="types">
<item>Exact Alarm</item>
<item>Inexact Alarm</item>
<item>JobScheduler</item>
</string-array>
<string-array name="periods">
<item>1 Minute</item>
<item>15 Minutes</item>
<item>30 Minutes</item>
<item>60 Minutes</item>
</string-array>
<string-array name="types">
<item>Exact Alarm</item>
<item>Inexact Alarm</item>
<item>JobScheduler</item>
</string-array>
<string-array name="periods">
<item>1 Minute</item>
<item>15 Minutes</item>
<item>30 Minutes</item>
<item>60 Minutes</item>
</string-array>
</resources>

0 comments on commit cda9354

Please sign in to comment.