Skip to content

Commit

Permalink
Convert KillerManager to kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
mklkj committed May 17, 2020
1 parent 7b379e1 commit 0060b7a
Show file tree
Hide file tree
Showing 19 changed files with 196 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.appcompat.widget.AppCompatCheckBox;

import com.thelittlefireman.appkillermanager.managers.KillerManager;
import com.thelittlefireman.appkillermanager.managers.NoActionFoundException;
import com.thelittlefireman.appkillermanager.ui.DialogKillerManagerBuilder;

import timber.log.Timber;
Expand All @@ -21,7 +22,7 @@ public class MainActivity extends Activity {
Button notificationManagerButton;
AppCompatCheckBox mAppCompatCheckBoxByDialog;

KillerManager.Actions currentAction = null;
KillerManager.Action currentAction = null;



Expand All @@ -44,13 +45,13 @@ protected void onCreate(final Bundle savedInstanceState) {
powerSavingManagerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentAction = KillerManager.Actions.ACTION_POWERSAVING;
currentAction = KillerManager.Action.ACTION_POWER_SAVING;
if (mAppCompatCheckBoxByDialog.isChecked()) {
startDialog(currentAction);
} else {
try {
KillerManager.doActionPowerSaving(MainActivity.this);
} catch (KillerManager.NoActionFoundException e) {
} catch (NoActionFoundException e) {
Timber.e(e);
}
}
Expand All @@ -59,13 +60,13 @@ public void onClick(View v) {
autoStartManagerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentAction = KillerManager.Actions.ACTION_AUTOSTART;
currentAction = KillerManager.Action.ACTION_AUTO_START;
if (mAppCompatCheckBoxByDialog.isChecked()) {
startDialog(currentAction);
} else {
try {
KillerManager.doActionAutoStart(MainActivity.this);
} catch (KillerManager.NoActionFoundException e) {
} catch (NoActionFoundException e) {
Timber.e(e);
}
}
Expand All @@ -74,13 +75,13 @@ public void onClick(View v) {
notificationManagerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
currentAction = KillerManager.Actions.ACTION_NOTIFICATIONS;
currentAction = KillerManager.Action.ACTION_NOTIFICATIONS;
if (mAppCompatCheckBoxByDialog.isChecked()) {
startDialog(currentAction);
} else {
try {
KillerManager.doActionNotification(MainActivity.this);
} catch (KillerManager.NoActionFoundException e) {
} catch (NoActionFoundException e) {
Timber.e(e);
}
}
Expand All @@ -89,15 +90,15 @@ public void onClick(View v) {

if (KillerManager.isDeviceSupported()) {
isDeviceSupported.setText("Is supported: true");
deviceName.setText("Device name: ".concat(KillerManager.getDevice().getDeviceManufacturer().toString()));
deviceName.setText("Device name: ".concat(KillerManager.getDevice().getManufacturer().toString()));
}

}

public void startDialog(KillerManager.Actions actions) {
public void startDialog(KillerManager.Action action) {

try {
new DialogKillerManagerBuilder(this).setAction(actions).show();
new DialogKillerManagerBuilder(this).setAction(action).show();

} catch (DialogKillerManagerBuilder.UnAvailableActionException e) {
Timber.e(e);
Expand All @@ -111,17 +112,15 @@ public void startDialog(KillerManager.Actions actions) {
public void onResume() {

super.onResume();
if (currentAction == KillerManager.Actions.ACTION_AUTOSTART) {
if (currentAction == KillerManager.Action.ACTION_AUTO_START) {
// may show dalog to ask the user about the result of action
// and store the result in preference
} else if (currentAction == KillerManager.Actions.ACTION_NOTIFICATIONS) {
} else if (currentAction == KillerManager.Action.ACTION_NOTIFICATIONS) {

} else if (currentAction == KillerManager.Actions.ACTION_POWERSAVING) {
} else if (currentAction == KillerManager.Action.ACTION_POWER_SAVING) {

}
// dont forget to nullify current action
currentAction = null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class Asus : Device {
}

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.ASUS

override val helpImageAutoStart: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Device {

val isThatRom: Boolean

val deviceManufacturer: Manufacturer
val manufacturer: Manufacturer

@get:DrawableRes
val helpImagePowerSaving: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ class HTC : Device {
}

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.HTC

override fun getActionPowerSaving(context: Context): Intent = ActionsUtils.createIntent().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class Huawei : Device {
"EmotionUI_3.0.1".equals(getEmuiRomName(), ignoreCase = true) ||
"EmotionUI_3.1".equals(getEmuiRomName(), ignoreCase = true) ||
"EmotionUI_4.1".equals(getEmuiRomName(), ignoreCase = true) ||
Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.HUAWEI

override val helpImagePowerSaving: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import com.thelittlefireman.appkillermanager.utils.Manufacturer
class Letv : Device {

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.LETV

override fun getActionPowerSaving(context: Context): Intent = ActionsUtils.createIntent().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class Meizu : Device {
}

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.MEIZU

override fun isActionPowerSavingAvailable(context: Context) = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import com.thelittlefireman.appkillermanager.utils.Manufacturer
class OnePlus : Device {

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

// This is mandatory for new oneplus version android 8
override fun needToUseAlongWithActionDoseMode(): Boolean = true

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.ONEPLUS

override fun getActionAutoStart(context: Context): Intent = ActionsUtils.createIntent().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Oppo : Device {
}

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.OPPO

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,16 @@ class Samsung : Device {
get() = R.drawable.samsung

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.SAMSUNG

// SmartManager is not available before lollipop version
override fun isActionPowerSavingAvailable(context: Context) = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP

override fun isActionAutoStartAvailable(context: Context) = false

override fun isActionNotificationAvailable(context: Context) = false

override fun needToUseAlongWithActionDoseMode() = true

override fun getActionPowerSaving(context: Context): Intent? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Vivo : Device {
override val isThatRom: Boolean
get() = false

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.VIVO

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class Xiaomi : Device {
private const val MIUI_ACTION_POWER_SAVE_EXTRA_LABEL = "package_label"
}

override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.XIAOMI

override val isThatRom: Boolean
get() {
return Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
return Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)
}

override fun isActionPowerSavingAvailable(context: Context) = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class ZTE : Device {
}

override val isThatRom: Boolean
get() = Build.BRAND.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(deviceManufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(deviceManufacturer.toString(), ignoreCase = true)
get() = Build.BRAND.equals(manufacturer.toString(), ignoreCase = true) ||
Build.MANUFACTURER.equals(manufacturer.toString(), ignoreCase = true) ||
Build.FINGERPRINT.contains(manufacturer.toString(), ignoreCase = true)


override val deviceManufacturer: Manufacturer
override val manufacturer: Manufacturer
get() = Manufacturer.ZTE

override fun isActionPowerSavingAvailable(context: Context) = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ object DevicesManager {

@JvmStatic
fun getDevice(): Device? {
val currentDeviceBase = devices.filter { it.isThatRom }
val currentDevice = devices.filter { it.isThatRom }

if (currentDeviceBase.size > 1) {
val logDevices = currentDeviceBase.joinToString(", ") {
it.deviceManufacturer.toString()
if (currentDevice.size > 1) {
val logDevices = currentDevice.joinToString(", ") {
it.manufacturer.toString()
}

Timber.w("More than one corresponding device: %s. Debug info: %s", logDevices, SystemUtils.getDefaultDebugInformation())
}

return currentDeviceBase.firstOrNull()
return currentDevice.firstOrNull()
}
}
Loading

0 comments on commit 0060b7a

Please sign in to comment.