112 lines
4.4 KiB
Java
112 lines
4.4 KiB
Java
package com.aldo.apps.familyhelpers;
|
|
|
|
import static android.Manifest.permission.POST_NOTIFICATIONS;
|
|
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.activity.result.ActivityResultLauncher;
|
|
import androidx.activity.result.contract.ActivityResultContracts;
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import com.aldo.apps.familyhelpers.ui.HelperGroupTile;
|
|
import com.aldo.apps.familyhelpers.ui.SleepTimerPopup;
|
|
import com.aldo.apps.familyhelpers.utils.DevicePolicyManagerHelper;
|
|
|
|
/**
|
|
* The Activity showing the Grid of helpers to select from.
|
|
*/
|
|
public class HelperGridActivity extends AppCompatActivity {
|
|
|
|
/**
|
|
* Tag for debugging purpose.
|
|
*/
|
|
private static final String TAG = "HelperGridActivity";
|
|
|
|
/**
|
|
* {@link HelperGroupTile} holding the option for a sleep timer.
|
|
*/
|
|
private HelperGroupTile mSleepTimerTile;
|
|
|
|
/**
|
|
* Instance of the {@link DevicePolicyManagerHelper} to roll out device specific actions.
|
|
*/
|
|
private DevicePolicyManagerHelper mDevicePolicyHelper;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
mDevicePolicyHelper = DevicePolicyManagerHelper.getInstance(this);
|
|
initSleepTimer();
|
|
} /**
|
|
* The {@link ActivityResultLauncher} to ask for the NotificationPermission.
|
|
*/
|
|
private final ActivityResultLauncher<String> mRequestPermissionLauncher =
|
|
registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
|
|
if (isGranted) {
|
|
// Permission granted, you can post notifications
|
|
mSleepTimerTile.launchHelper();
|
|
} else {
|
|
// Permission denied, handle accordingly
|
|
Toast.makeText(HelperGridActivity.this, R.string.sleep_timer_show_notifications_rationale,
|
|
Toast.LENGTH_LONG).show();
|
|
requestNotificationPermission();
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Helper method to initialize the sleepTimer tile.
|
|
*/
|
|
private void initSleepTimer() {
|
|
mSleepTimerTile = new HelperGroupTile(findViewById(R.id.tile_sleep_timer)) {
|
|
@Override
|
|
public void launchHelper() {
|
|
// First check if the notification permission is granted
|
|
if (!requestNotificationPermission()) {
|
|
Log.d(TAG, "launchHelper: Notifications not allowed, return...");
|
|
return;
|
|
}
|
|
// Second check for the app to be registered as DeviceAdmin
|
|
if (!mDevicePolicyHelper.isDeviceAdmin()) {
|
|
Toast.makeText(HelperGridActivity.this, R.string.warning_no_device_admin,
|
|
Toast.LENGTH_LONG).show();
|
|
mDevicePolicyHelper.requestDeviceAdminPrivileges(HelperGridActivity.this,
|
|
R.string.sleep_timer_rationale_device_admin);
|
|
return;
|
|
}
|
|
// If both previous checks pass, launch the actual sleep timer dialog.
|
|
final SleepTimerPopup sleepTimerPopup = new SleepTimerPopup(HelperGridActivity.this);
|
|
sleepTimerPopup.showTimePicker();
|
|
Log.d(TAG, "launchHelper: Clicked SleepTimer");
|
|
}
|
|
};
|
|
mSleepTimerTile.setLogo(R.drawable.icn_sleep_timer);
|
|
mSleepTimerTile.setTitle(R.string.title_sleep_timer);
|
|
}
|
|
|
|
/**
|
|
* Helper method to request the NotificationPermission as it is required for the service to run.
|
|
*
|
|
* @return true if the permission is granted, false otherwise.
|
|
*/
|
|
private boolean requestNotificationPermission() {
|
|
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) ==
|
|
PackageManager.PERMISSION_GRANTED) {
|
|
// Permission already granted
|
|
return true;
|
|
} else {
|
|
if (shouldShowRequestPermissionRationale(POST_NOTIFICATIONS)) {
|
|
Toast.makeText(HelperGridActivity.this, R.string.sleep_timer_show_notifications_rationale,
|
|
Toast.LENGTH_LONG).show();
|
|
}
|
|
mRequestPermissionLauncher.launch(POST_NOTIFICATIONS);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
} |