Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
wuan committed May 31, 2018
1 parent c714038 commit 214ff23
Show file tree
Hide file tree
Showing 50 changed files with 154 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ data class AlertParameters(
val rangeSteps: Array<Float>,
val sectorLabels: Array<String>,
val measurementSystem: MeasurementSystem
) {
}
)

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ data class AlertSector(
val maximumSectorBearing: Float,
val ranges: List<AlertSectorRange>,
val closestStrikeDistance: Float
) {}
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ data class AlertSectorRange(
val rangeMaximum: Float,
val strikeCount: Int,
val latestStrikeTimestamp: Long
) {}
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ import android.net.Uri
data class AlertSignal(
val vibrationDuration: Int = 0,
val soundSignal: Uri? = null
) {
}
)

Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ open class AlertDataHandler internal constructor(
val distance = calculateDistanceTo(location, strike, measurementSystem)

sector.ranges.find { distance <= it.rangeMaximum }?.let {
it.addStrike(strike);
it.addStrike(strike)
if (strike.timestamp >= thresholdTime) {
sector.updateClosestStrikeDistance(distance)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import org.jetbrains.anko.uiThread

class AlertHandler(
private val locationHandler: LocationHandler,
private val preferences: SharedPreferences,
preferences: SharedPreferences,
private val context: Context,
private val vibrator: Vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator,
private val notificationHandler: NotificationHandler = NotificationHandler(context),
Expand Down Expand Up @@ -282,6 +282,7 @@ class AlertHandler(
if (isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
ringtone.audioAttributes = AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_NOTIFICATION).build()
} else {
@Suppress("DEPRECATION")
ringtone.streamType = AudioManager.STREAM_NOTIFICATION
}
ringtone.play()
Expand Down
13 changes: 10 additions & 3 deletions app/src/main/java/org/blitzortung/android/app/AppService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Binder
import android.os.Build
import android.os.Handler
import android.os.IBinder
import android.util.Log
Expand All @@ -40,6 +41,7 @@ import org.blitzortung.android.data.provider.result.StatusEvent
import org.blitzortung.android.location.LocationHandler
import org.blitzortung.android.util.LogUtil
import org.blitzortung.android.util.Period
import org.blitzortung.android.util.isAtLeast
import java.util.*

class AppService protected constructor(private val handler: Handler, private val updatePeriod: Period) : Service(), Runnable, SharedPreferences.OnSharedPreferenceChangeListener {
Expand All @@ -60,7 +62,6 @@ class AppService protected constructor(private val handler: Handler, private val

private val dataHandler: DataHandler = BOApplication.dataHandler
private val locationHandler: LocationHandler = BOApplication.locationHandler
private val alertHandler: AlertHandler = BOApplication.alertHandler

private val preferences = BOApplication.sharedPreferences

Expand Down Expand Up @@ -131,7 +132,11 @@ class AppService protected constructor(private val handler: Handler, private val
synchronized(wakeLock) {
if (!wakeLock.isHeld) {
Log.v(Main.LOG_TAG, "AppService.acquireWakeLock() before: $wakeLock")
wakeLock.acquire()
if (isAtLeast(Build.VERSION_CODES.N)) {
wakeLock.acquire(WAKELOCK_TIMEOUT)
} else {
wakeLock.acquire()
}
return true
} else {
Log.v(Main.LOG_TAG, "AppService.acquireWakeLock() skip")
Expand Down Expand Up @@ -211,6 +216,7 @@ class AppService protected constructor(private val handler: Handler, private val
}

private fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: PreferenceKey) {
@Suppress("NON_EXHAUSTIVE_WHEN")
when (key) {
PreferenceKey.ALERT_ENABLED -> {
alertEnabled = sharedPreferences.get(key, false)
Expand Down Expand Up @@ -315,7 +321,8 @@ class AppService protected constructor(private val handler: Handler, private val
}

companion object {
val RETRIEVE_DATA_ACTION = "retrieveData"
const val RETRIEVE_DATA_ACTION = "retrieveData"
const val WAKELOCK_TIMEOUT = 5000L

var instance: AppService? = null
private set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class BOApplication : Application() {

wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKE_LOCK_TAG)

dataHandler = DataHandler(applicationContext, wakeLock, "-${getPackageInfo().versionCode.toString()}")
dataHandler = DataHandler(applicationContext, wakeLock, "-${getPackageInfo().versionCode}")

locationHandler = LocationHandler(applicationContext, sharedPreferences)

Expand Down
30 changes: 8 additions & 22 deletions app/src/main/java/org/blitzortung/android/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {

hideActionBar()

buttonColumnHandler = ButtonColumnHandler<ImageButton, ButtonGroup>(if (TabletAwareView.isTablet(this)) 75f else 55f)
buttonColumnHandler = ButtonColumnHandler(if (TabletAwareView.isTablet(this)) 75f else 55f)
configureMenuAccess()
historyController = HistoryController(this, buttonColumnHandler)

Expand Down Expand Up @@ -244,20 +244,6 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
bindService(serviceIntent, serviceConnection, 0)
}

private fun setupDetailModeButton() {
with(toggleExtendedMode) {
isEnabled = true
visibility = View.VISIBLE

setOnClickListener {
BOApplication.dataHandler.toggleExtendedMode()
reloadData()
}

buttonColumnHandler.addElement(this, ButtonGroup.DATA_UPDATING)
}
}

private fun setupCustomViews() {
with(legend_view) {
strikesOverlay = this@Main.strikeListOverlay
Expand Down Expand Up @@ -291,7 +277,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {

with(histogram_view) {
setStrikesOverlay(strikeListOverlay)
setOnClickListener { view ->
setOnClickListener { _ ->
val currentResult = currentResult
if (currentResult != null) {
val rasterParameters = currentResult.rasterParameters
Expand Down Expand Up @@ -495,7 +481,7 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
Log.i(LOG_TAG, "$providerName permission has now been granted.")
val editor = preferences.edit()
editor.put(PreferenceKey.LOCATION_MODE, providerName)
editor.commit()
editor.apply()
} else {
Log.i(LOG_TAG, "$providerName permission was NOT granted.")
}
Expand Down Expand Up @@ -529,21 +515,21 @@ class Main : OwnMapActivity(), OnSharedPreferenceChangeListener {
if (backgroundPeriod > 0) {
val pm = context.getSystemService(Context.POWER_SERVICE)
if (pm is PowerManager) {
val packageName = context.packageName;
val packageName = context.packageName
Log.v(LOG_TAG, "requestWakeupPermissions() package name $packageName")
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
val locationText = context.resources.getString(R.string.open_battery_optimiziation)

val dialogClickListener = DialogInterface.OnClickListener { dialog, which ->
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
Toast.makeText(baseContext, R.string.background_query_toast, Toast.LENGTH_LONG).show()

Log.v(LOG_TAG, "requestWakeupPermissions() request ignore battery optimizations")
val intent = Intent();
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
val intent = Intent()
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
try {
context.startActivity(intent);
context.startActivity(intent)
} catch (e: AndroidRuntimeException) {
Log.e(LOG_TAG, "requestWakeupPermissions() could not open battery optimization settings", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.blitzortung.android.dialogs.InfoDialog
import org.blitzortung.android.dialogs.LogDialog
import org.jetbrains.anko.startActivity

class MainPopupMenu(private val context: Context, anchor: View) : PopupMenu(context, anchor) {
class MainPopupMenu(context: Context, anchor: View) : PopupMenu(context, anchor) {

init {
setOnMenuItemClickListener(ClickListener(context))
Expand All @@ -28,7 +28,7 @@ class MainPopupMenu(private val context: Context, anchor: View) : PopupMenu(cont
val dialog = when (item?.itemId) {
R.id.menu_info -> InfoDialog(context, versionComponent)

R.id.menu_alarms -> AlertDialog(context, AppService.instance, AlertDialogColorHandler(BOApplication.sharedPreferences))
R.id.menu_alarms -> AlertDialog(context, AlertDialogColorHandler(BOApplication.sharedPreferences))

R.id.menu_log -> LogDialog(context, versionComponent)

Expand Down
6 changes: 0 additions & 6 deletions app/src/main/java/org/blitzortung/android/app/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ class Preferences : PreferenceActivity(), OnSharedPreferenceChangeListener {
onSharedPreferenceChanged(sharedPreferences, PreferenceKey.fromString(keyString))
}

private fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, vararg keys: PreferenceKey) {
for (key in keys) {
onSharedPreferenceChanged(sharedPreferences, key)
}
}

private fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: PreferenceKey) {
when (key) {
PreferenceKey.DATA_SOURCE -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import org.blitzortung.android.app.view.AlertView
class WidgetProvider : AppWidgetProvider() {

override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
val N = appWidgetIds.size
for (i in 0..N - 1) {
for (i in 0..appWidgetIds.size - 1) {
val appWidgetId = appWidgetIds[i]
updateAppWidget(context)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.blitzortung.android.app.helper.ViewHelper
class ButtonColumnHandler<V : View, G : Enum<G>>(private val buttonSize: Float) {

data class GroupedView<V, G>(val view: V, val groups: Set<G>) {
constructor(view: V, vararg groups: G) : this(view, groups.toSet()) {}
constructor(view: V, vararg groups: G) : this(view, groups.toSet())
}

private val elements: MutableList<GroupedView<V, G>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ open class NotificationHandler(private val context: Context) {

if (isAtLeast(Build.VERSION_CODES.JELLY_BEAN_MR1)) {
builder.setWhen(System.currentTimeMillis())
.setShowWhen(true);
.setShowWhen(true)
}

return builder.build()
Expand All @@ -79,7 +79,4 @@ open class NotificationHandler(private val context: Context) {
return notification
}

fun clearNotification() {
notificationService?.cancel(R.id.alarm_notification_id)
}
}
11 changes: 1 addition & 10 deletions app/src/main/java/org/blitzortung/android/app/view/AlertView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import org.blitzortung.android.alert.AlertResult
import org.blitzortung.android.alert.data.AlertSector
import org.blitzortung.android.alert.event.AlertEvent
import org.blitzortung.android.alert.event.AlertResultEvent
import org.blitzortung.android.app.AppService
import org.blitzortung.android.app.BOApplication
import org.blitzortung.android.app.Main
import org.blitzortung.android.app.R
Expand Down Expand Up @@ -96,7 +95,7 @@ class AlertView @JvmOverloads constructor(
background.color = 0xffb0b0b0.toInt()

setOnLongClickListener {
AlertDialog(context, AppService.instance, AlertDialogColorHandler(BOApplication.sharedPreferences))
AlertDialog(context, AlertDialogColorHandler(BOApplication.sharedPreferences))
.show()

true
Expand All @@ -118,14 +117,6 @@ class AlertView @JvmOverloads constructor(
super.onMeasure(View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(size, View.MeasureSpec.EXACTLY))
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
}

override fun onDraw(canvas: Canvas) {
val size = Math.max(width, height)
val pad = 4
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/java/org/blitzortung/android/app/view/LegendView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ class LegendView @JvmOverloads constructor(
}

private fun determineWidth(intervalDuration: Int): Float {
var innerWidth = colorFieldSize + padding + textPaint.measureText(if (intervalDuration > 100) "< 100min" else "< 10min").toFloat()
var innerWidth = colorFieldSize + padding + textPaint.measureText(if (intervalDuration > 100) "< 100min" else "< 10min")

if (hasRegion()) {
innerWidth = Math.max(innerWidth, regionTextPaint.measureText(regionName).toFloat())
innerWidth = Math.max(innerWidth, regionTextPaint.measureText(regionName))
}

return padding + innerWidth + padding
Expand Down Expand Up @@ -201,8 +201,8 @@ class LegendView @JvmOverloads constructor(
}

companion object {
val REGION_HEIGHT = 1.1f
val RASTER_HEIGHT = 0.8f
val COUNT_THRESHOLD_HEIGHT = 0.8f
const val REGION_HEIGHT = 1.1f
const val RASTER_HEIGHT = 0.8f
const val COUNT_THRESHOLD_HEIGHT = 0.8f
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package org.blitzortung.android.app.view

import android.content.Context
import android.util.AttributeSet
import android.widget.TextView
import org.blitzortung.android.app.R
import org.blitzortung.android.util.TabletAwareView

class ScaledTextView(context: Context, attrs: AttributeSet?) : TextView(context, attrs) {
class ScaledTextView(context: Context, attrs: AttributeSet?) : android.support.v7.widget.AppCompatTextView(context, attrs) {

init {
val a = context.obtainStyledAttributes(attrs, R.styleable.View, 0, 0);
val a = context.obtainStyledAttributes(attrs, R.styleable.View, 0, 0)

val scaleForTablet = a.getBoolean(R.styleable.View_tablet_scaleable, false) && TabletAwareView.isTablet(context)

if (scaleForTablet) {
val displayMetrics = context.resources.displayMetrics
textSize *= TabletAwareView.textSizeFactor(scaleForTablet) / displayMetrics.scaledDensity
}
a.recycle()
}
}
Loading

0 comments on commit 214ff23

Please sign in to comment.