Skip to content
This repository has been archived by the owner on Dec 3, 2021. It is now read-only.

Commit

Permalink
feat: Add stop and start service buttons
Browse files Browse the repository at this point in the history
Fix job scheduler when user choose Never sync.
close #45
  • Loading branch information
aoudiamoncef committed Mar 11, 2018
1 parent 4dfa1b6 commit 8fa9366
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 22 deletions.
19 changes: 12 additions & 7 deletions app/src/main/java/science/apolline/utils/SyncJobScheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ object SyncJobScheduler : AnkoLogger {
private lateinit var executor: ScheduledFuture<*>

fun setAutoSync(time: Long, context: Context) {
executor = scheduler.scheduleAtFixedRate(
{ syncTask(context) },
1,
time,
TimeUnit.MINUTES)
info("setAutoSync: "+ executor.isDone)
if (time > 0L) {
executor = scheduler.scheduleAtFixedRate(
{ syncTask(context) },
1,
time,
TimeUnit.MINUTES)
info("setAutoSync: " + executor.isDone)
}else{
cancelAutoSync(true)
info("cancelAutoSync disabled by user")
}
}

fun cancelAutoSync(interrupt: Boolean): Boolean {
if (!(executor.isDone || executor.isCancelled)) {
executor.cancel(interrupt)
return true
}
info("cancelAutoSync: "+ executor.isCancelled)
info("cancelAutoSync: " + executor.isCancelled)
return false
}

Expand Down
36 changes: 23 additions & 13 deletions app/src/main/java/science/apolline/view/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class MainActivity : RootActivity(), NavigationView.OnNavigationItemSelectedList
navigationView.setNavigationItemSelectedListener(this)

// Preferences.
mPrefs = PreferenceManager.getDefaultSharedPreferences(this)
mPrefs = PreferenceManager.getDefaultSharedPreferences(this)
//mPrefs = this.getSharedPreferences( IDENTIFIER, Context.MODE_PRIVATE)
INFLUXDB_SYNC_FREQ = (mPrefs.getString("sync_frequency","60")).toLong()
INFLUXDB_SYNC_FREQ = (mPrefs.getString("sync_frequency", "60")).toLong()


mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
Expand Down Expand Up @@ -120,16 +120,27 @@ class MainActivity : RootActivity(), NavigationView.OnNavigationItemSelectedList

override fun onOptionsItemSelected(item: MenuItem?): Boolean {

if (CheckUtility.isNetworkConnected(this)) {
mJobManager.addJobInBackground(SyncInfluxDBJob())
info(mPrefs.all)
info("sync"+ mPrefs.getLong("sync_frequency ",60L).toString())
info("sync after $INFLUXDB_SYNC_FREQ")
when (item?.itemId) {
R.id.sync -> {
if (CheckUtility.isNetworkConnected(this)) {
mJobManager.addJobInBackground(SyncInfluxDBJob())
Toasty.info(applicationContext, "Synchronization in progress", Toast.LENGTH_SHORT, true).show()
} else {
mJobManager.addJobInBackground(SyncInfluxDBJob())
Toasty.warning(applicationContext, "No internet connection ! Synchronization job added to queue", Toast.LENGTH_LONG, true).show()
}
return true
}

Toasty.info(applicationContext, "Synchronization in progress", Toast.LENGTH_SHORT, true).show()
} else {
mJobManager.addJobInBackground(SyncInfluxDBJob())
Toasty.warning(applicationContext, "No internet connection ! Synchronization job added to queue", Toast.LENGTH_LONG, true).show()
R.id.start -> {
stopService(Intent(applicationContext, IOIOService::class.java))
startService(Intent(applicationContext, IOIOService::class.java))
return true
}
R.id.pause -> {
stopService(Intent(applicationContext, IOIOService::class.java))
return true
}
}
return true
}
Expand All @@ -144,7 +155,7 @@ class MainActivity : RootActivity(), NavigationView.OnNavigationItemSelectedList
val viewPagerFragment = ViewPagerFragment()
replaceFragment(viewPagerFragment)
}
R.id.grp_fonction-> if (itemId == R.id.nav_setting) {
R.id.grp_fonction -> if (itemId == R.id.nav_setting) {
val intent = Intent(this, SettingsActivity::class.java)
startActivity(intent)
}
Expand Down Expand Up @@ -256,7 +267,6 @@ class MainActivity : RootActivity(), NavigationView.OnNavigationItemSelectedList
private val PERMISSIONS_ARRAY = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE)
private const val REQUEST_CODE_PERMISSIONS_ARRAY = 100
private const val REQUEST_CODE_ENABLE_BLUETOOTH = 101
private const val IDENTIFIER = "science.apolline"

}
}
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_pause_circle_filled.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFF"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM11,16L9,16L9,8h2v8zM15,16h-2L13,8h2v8z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_play_circle_filled.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM10,16.5v-9l6,4.5 -6,4.5z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_sync.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFF"
android:pathData="M12,4L12,1L8,5l4,4L12,6c3.31,0 6,2.69 6,6 0,1.01 -0.25,1.97 -0.7,2.8l1.46,1.46C19.54,15.03 20,13.57 20,12c0,-4.42 -3.58,-8 -8,-8zM12,18c-3.31,0 -6,-2.69 -6,-6 0,-1.01 0.25,-1.97 0.7,-2.8L5.24,7.74C4.46,8.97 4,10.43 4,12c0,4.42 3.58,8 8,8v3l4,-4 -4,-4v3z"/>
</vector>
16 changes: 14 additions & 2 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,20 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_location"
android:icon="@drawable/ic_cloud_upload_white_48dp"
android:id="@+id/start"
android:icon="@drawable/ic_play_circle_filled"
android:title="@string/app_name"
app:showAsAction="ifRoom">
</item>
<item
android:id="@+id/pause"
android:icon="@drawable/ic_pause_circle_filled"
android:title="@string/app_name"
app:showAsAction="ifRoom">
</item>
<item
android:id="@+id/sync"
android:icon="@drawable/ic_sync"
android:title="@string/app_name"
app:showAsAction="ifRoom"/>
</menu>

0 comments on commit 8fa9366

Please sign in to comment.