Skip to content

Commit

Permalink
added arrayadapter, not working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
hhaasbroek committed Aug 16, 2018
1 parent 17d432b commit 63642cd
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 381 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
package="dpm.location.tracker">

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application
android:name=".room.LocationRepository"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".room.LocationRepository"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
Expand All @@ -31,6 +30,8 @@
android:name=".LocationUpdatesService"
android:label="My Job Service Update "
android:permission="android.permission.BIND_JOB_SERVICE" />

<activity android:name=".ListViewActivity"></activity>
</application>

</manifest>
289 changes: 158 additions & 131 deletions app/src/main/java/dpm/location/tracker/JobServiceDemoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import dpm.location.tracker.room.LocationAdapter
import dpm.location.tracker.room.LocationDb
import dpm.location.tracker.room.LocationRepository
import dpm.location.tracker.room.storedLoctation
import dpm.location.tracker.room.StoredLoctation
import kotlinx.android.synthetic.main.content_job_service_demo.delete_data
import kotlinx.android.synthetic.main.content_job_service_demo.show_results
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread
import java.text.DateFormat
Expand All @@ -35,6 +40,8 @@ class JobServiceDemoActivity : AppCompatActivity(), NavigationView.OnNavigationI

// as google doc says
// Handler for incoming messages from the service.
private var listView: ListView? = null
private val mAdapter: LocationAdapter? = null
private var mHandler: IncomingMessageHandler? = null

private val db: LocationDb
Expand All @@ -52,181 +59,201 @@ class JobServiceDemoActivity : AppCompatActivity(), NavigationView.OnNavigationI
val obj = msg.obj as Location
val currentDateTimeString = DateFormat.getDateTimeInstance().format(Date())
locationMsg!!.text = "LAT : " + obj.latitude + "\nLNG : " + obj.longitude + "\n\n" + obj.toString() + " \n\n\nLast updated- " + currentDateTimeString
val location = StoredLoctation()
location.lat = obj.latitude
location.lon = obj.longitude
val list = ArrayList<StoredLoctation>()
list.add(location)
doAsync {
db.locationDao().insertAll(list)
uiThread {

}
}
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_job_service_demo)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)

//room
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_job_service_demo)
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)

val list = ArrayList<storedLoctation>()
for (i in 0..9) {
val location = storedLoctation()
location.lat = "2"
location.lon = "1"
list.add(location)
}
doAsync {
val products = db.locationDao().all
uiThread {
//room

}
}
// List<StoredLoctation> products = LocationRepository.get().getDB().locationDao().getAll();

doAsync {
val products = db.locationDao().all
uiThread {
val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
val toggle = ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer.addDrawerListener(toggle)
toggle.syncState()

}
val navigationView = findViewById<NavigationView>(R.id.nav_view)
navigationView.setNavigationItemSelectedListener(this)

drawer.setDrawerLockMode(LOCK_MODE_LOCKED_CLOSED)

locationMsg = findViewById(R.id.location)

mHandler = IncomingMessageHandler()
requestPermissions()
bindListeners()
}

// List<storedLoctation> products = LocationRepository.get().getDB().locationDao().getAll();
override fun onStart() {
super.onStart()
}

val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
val toggle = ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer.addDrawerListener(toggle)
toggle.syncState()
override fun onDestroy() {
super.onDestroy()
mHandler = null
}

val navigationView = findViewById<NavigationView>(R.id.nav_view)
navigationView.setNavigationItemSelectedListener(this)
/**
* Callback received when a permissions request has been completed.
*/
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
grantResults: IntArray) {
Log.i(TAG, "onRequestPermissionResult")
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.size <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.i(TAG, "User interaction was cancelled.")
finish()
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// can be schedule in this way also
// Utils.scheduleJob(this, LocationUpdatesService.class);
//doing this way to communicate via messenger
// Start service and provide it a way to communicate with this class.
val startServiceIntent = Intent(this, LocationUpdatesService::class.java)
val messengerIncoming = Messenger(mHandler)
startServiceIntent.putExtra(MESSENGER_INTENT_KEY, messengerIncoming)
startService(startServiceIntent)
} else {
// Permission denied.
finish()
}
}
}
fun bindListeners(){
delete_data.setOnClickListener { deleteDb() }
show_results.setOnClickListener { showResults() }
}

drawer.setDrawerLockMode(LOCK_MODE_LOCKED_CLOSED)
private fun showResults() {
startActivity(Intent(this, ListViewActivity::class.java))
// doAsync {
// val products = db.locationDao().all
//
// uiThread {
//
// }
// }

locationMsg = findViewById(R.id.location)

mHandler = IncomingMessageHandler()

requestPermissions()
}

override fun onStart() {
super.onStart()
}
private fun deleteDb() {
doAsync {
//val products = db.locationDao().nukeTable()
uiThread {

override fun onDestroy() {
super.onDestroy()
mHandler = null
}

}
Toast.makeText(applicationContext,"Database deleted",Toast.LENGTH_LONG).show()
}

/**
* Callback received when a permissions request has been completed.
*/
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
grantResults: IntArray) {
Log.i(TAG, "onRequestPermissionResult")
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.size <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
Log.i(TAG, "User interaction was cancelled.")
finish()
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// can be schedule in this way also
// Utils.scheduleJob(this, LocationUpdatesService.class);
//doing this way to communicate via messenger
// Start service and provide it a way to communicate with this class.
val startServiceIntent = Intent(this, LocationUpdatesService::class.java)
val messengerIncoming = Messenger(mHandler)
startServiceIntent.putExtra(MESSENGER_INTENT_KEY, messengerIncoming)
startService(startServiceIntent)
override fun onBackPressed() {
val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START)
} else {
// Permission denied.
finish()
super.onBackPressed()
}
}
}

override fun onBackPressed() {
val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.job_service_demo, menu)
return true
}
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.job_service_demo, menu)
return true
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
val id = item.itemId

override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
val id = item.itemId
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {

if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {

} else if (id == R.id.nav_send) {
}

val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
drawer.closeDrawer(GravityCompat.START)
return true
}

val drawer = findViewById<DrawerLayout>(R.id.drawer_layout)
drawer.closeDrawer(GravityCompat.START)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId


return if (id == R.id.action_settings) {
true
} else super.onOptionsItemSelected(item)
}
return if (id == R.id.action_settings) {
true
} else super.onOptionsItemSelected(item)
}

private fun requestPermissions() {
val shouldProvideRationale = ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)

// Provide an additional rationale to the user. This would happen if the user denied the
// request previously, but didn't check the "Don't ask again" checkbox.
if (shouldProvideRationale) {
Log.i(TAG, "Displaying permission rationale to provide additional context.")
// Request permission
ActivityCompat.requestPermissions(this@JobServiceDemoActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_PERMISSIONS_REQUEST_CODE)
} else {
Log.i(TAG, "Requesting permission")
// Request permission. It's possible this can be auto answered if device policy
// sets the permission in a given state or the user denied the permission
// previously and checked "Never ask again".
ActivityCompat.requestPermissions(this@JobServiceDemoActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_PERMISSIONS_REQUEST_CODE)
private fun requestPermissions() {
val shouldProvideRationale = ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)

// Provide an additional rationale to the user. This would happen if the user denied the
// request previously, but didn't check the "Don't ask again" checkbox.
if (shouldProvideRationale) {
Log.i(TAG, "Displaying permission rationale to provide additional context.")
// Request permission
ActivityCompat.requestPermissions(this@JobServiceDemoActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_PERMISSIONS_REQUEST_CODE)
} else {
Log.i(TAG, "Requesting permission")
// Request permission. It's possible this can be auto answered if device policy
// sets the permission in a given state or the user denied the permission
// previously and checked "Never ask again".
ActivityCompat.requestPermissions(this@JobServiceDemoActivity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
REQUEST_PERMISSIONS_REQUEST_CODE)
}
}
}

companion object {
companion object {

private val TAG = JobServiceDemoActivity::class.java.simpleName
private val TAG = JobServiceDemoActivity::class.java.simpleName

/**
* Code used in requesting runtime permissions.
*/
private val REQUEST_PERMISSIONS_REQUEST_CODE = 34
/**
* Code used in requesting runtime permissions.
*/
private val REQUEST_PERMISSIONS_REQUEST_CODE = 34

val MESSENGER_INTENT_KEY = "msg-intent-key"
val MESSENGER_INTENT_KEY = "msg-intent-key"

private val DATABASE_NAME = "LocaionDB"
private val DATABASE_NAME = "LocaionDB"
}
}
}
Loading

0 comments on commit 63642cd

Please sign in to comment.