Skip to content

Commit

Permalink
Merge pull request #6 from WrichikBasu/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
WrichikBasu authored Oct 31, 2020
2 parents f2a5fba + f14c7a8 commit 5df689d
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 175 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "in.basulabs.shakealarmclock"
minSdkVersion 21
targetSdkVersion 30
versionCode 6
versionName "1.2.3"
versionCode 7
versionName "1.2.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -129,20 +127,14 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

} else if (getIntent().getAction().equals(ACTION_NEW_ALARM_FROM_INTENT)) {

Log.e(this.getClass().getSimpleName(), "Received intent.");

Bundle data = getIntent().getExtras();

if (data == null) {

Log.e(this.getClass().getSimpleName(), "No extras received.");

setVariablesInViewModel();

} else {

Log.e(this.getClass().getSimpleName(), "Extras received.");

setVariablesInViewModel(MODE_NEW_ALARM,
data.getInt(BUNDLE_KEY_ALARM_HOUR),
data.getInt(BUNDLE_KEY_ALARM_MINUTE),
Expand Down Expand Up @@ -187,10 +179,9 @@ private void setVariablesInViewModel() {
viewModel.setIsSnoozeOn(sharedPreferences.getBoolean(SHARED_PREF_KEY_DEFAULT_SNOOZE_IS_ON, true));
viewModel.setIsRepeatOn(false);

viewModel.setAlarmToneUri(Uri.parse(sharedPreferences
.getString(SHARED_PREF_KEY_DEFAULT_ALARM_TONE_URI,
RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM)
.toString())));
String alarmTone = sharedPreferences.getString(SHARED_PREF_KEY_DEFAULT_ALARM_TONE_URI, null);

viewModel.setAlarmToneUri(alarmTone != null ? Uri.parse(alarmTone) : null);

viewModel.setAlarmType(ALARM_TYPE_SOUND_ONLY);

Expand Down Expand Up @@ -277,8 +268,7 @@ private void setVariablesInViewModel(int mode, int alarmHour, int alarmMinute, i
//----------------------------------------------------------------------------------------------------

/**
* Sets the ActionBar title as per the created fragment. Uses {@link #whichFragment} to determine the current
* fragment.
* Sets the ActionBar title as per the created fragment. Uses {@link #whichFragment} to determine the current fragment.
*/
private void setActionBarTitle() {
switch (whichFragment) {
Expand Down Expand Up @@ -421,4 +411,5 @@ public void onDialogPositiveClick(DialogFragment dialogFragment) {
this.finish();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_alarmslist);
setSupportActionBar(findViewById(R.id.toolbar));

Log.e(this.getClass().getSimpleName(), "Inside onCreate()");

alarmDatabase = AlarmDatabase.getInstance(this);
viewModel = new ViewModelProvider(this).get(ViewModel_AlarmsList.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {

int type;
if (intent.hasExtra(EXTRA_RINGTONE_TYPE)) {
type = (int) Objects.requireNonNull(intent.getExtras()).get(EXTRA_RINGTONE_TYPE);
//Log.e(this.getClass().getSimpleName(), "type found = " + type);
type = Objects.requireNonNull(intent.getExtras()).getInt(EXTRA_RINGTONE_TYPE);
} else {
type = TYPE_ALL;
}
Expand All @@ -125,7 +124,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
String uri = allTonesCursor.getString(URI_COLUMN_INDEX);

toneUriList.add(Uri.parse(uri + "/" + id));
//toneUriList.add(ringtoneManager.getRingtoneUri(allTonesCursor.getPosition()));
toneNameList.add(allTonesCursor.getString(TITLE_COLUMN_INDEX));
toneIdList.add(View.generateViewId());
} while (allTonesCursor.moveToNext());
Expand All @@ -134,51 +132,52 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
thread.start();

if (intent.hasExtra(EXTRA_RINGTONE_SHOW_DEFAULT)) {
showDefault = (boolean) Objects.requireNonNull(intent.getExtras())
.get(EXTRA_RINGTONE_SHOW_DEFAULT);
//Log.e(this.getClass().getSimpleName(), "showDefault = " + showDefault);
showDefault = Objects.requireNonNull(intent.getExtras()).getBoolean(EXTRA_RINGTONE_SHOW_DEFAULT);
} else {
showDefault = true;
}

if (intent.hasExtra(EXTRA_RINGTONE_SHOW_SILENT)) {
showSilent = (boolean) Objects.requireNonNull(intent.getExtras())
.get(EXTRA_RINGTONE_SHOW_SILENT);
showSilent = Objects.requireNonNull(intent.getExtras()).getBoolean(EXTRA_RINGTONE_SHOW_SILENT);
} else {
showSilent = false;
}

if (showDefault) {
if (intent.hasExtra(EXTRA_RINGTONE_DEFAULT_URI)) {
defaultUri =
Objects.requireNonNull(intent.getExtras())
.getParcelable(EXTRA_RINGTONE_DEFAULT_URI);
defaultUri = Objects.requireNonNull(intent.getExtras()).getParcelable(EXTRA_RINGTONE_DEFAULT_URI);
} else {
defaultUri = RingtoneManager.getActualDefaultRingtoneUri(this, type);
if (type == RingtoneManager.TYPE_ALARM) {
defaultUri = Settings.System.DEFAULT_ALARM_ALERT_URI;
} else if (type == RingtoneManager.TYPE_NOTIFICATION) {
defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI;
} else if (type == RingtoneManager.TYPE_RINGTONE) {
defaultUri = Settings.System.DEFAULT_RINGTONE_URI;
} else {
defaultUri = RingtoneManager.getActualDefaultRingtoneUri(this, type);
}
}
} else {
defaultUri = null;
}

if (intent.hasExtra(EXTRA_RINGTONE_EXISTING_URI)) {
existingUri = Objects.requireNonNull(intent.getExtras())
.getParcelable(EXTRA_RINGTONE_EXISTING_URI);
existingUri = Objects.requireNonNull(intent.getExtras()).getParcelable(EXTRA_RINGTONE_EXISTING_URI);
wasExistingUriGiven = true;
} else {
existingUri = null;
wasExistingUriGiven = false;
}

if (intent.hasExtra(EXTRA_RINGTONE_TITLE)) {
title = (CharSequence) Objects.requireNonNull(intent.getExtras())
.get(EXTRA_RINGTONE_TITLE);
title = (CharSequence) Objects.requireNonNull(intent.getExtras()).get(EXTRA_RINGTONE_TITLE);
} else {
title = "Select tone:";
}

if (intent.hasExtra(ConstantsAndStatics.EXTRA_PLAY_RINGTONE)) {
playTone = (boolean) Objects.requireNonNull(intent.getExtras())
.get(ConstantsAndStatics.EXTRA_PLAY_RINGTONE);
playTone = Objects.requireNonNull(intent.getExtras())
.getBoolean(ConstantsAndStatics.EXTRA_PLAY_RINGTONE);
} else {
playTone = true;
}
Expand Down Expand Up @@ -221,7 +220,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
protected void onResume() {
super.onResume();
//Log.e(this.getClass().getSimpleName(), "Inside onResume");
if (! isPermissionAvailable()) {
checkAndRequestPermission();
}
Expand All @@ -238,7 +236,6 @@ protected void onPause() {
}
}


//----------------------------------------------------------------------------------------------------

/**
Expand All @@ -262,43 +259,32 @@ private void populateRadioGroup() {

if (existingUri != null) {

//Log.e(this.getClass().getSimpleName(), "existingUri is NOT null.");

////////////////////////////////////////////////////////////////////
// As existingUri is not null, we are required to pre-select
// a specific RadioButton.
///////////////////////////////////////////////////////////////////

if (showDefault && existingUri.equals(defaultUri)) {

//Log.e(this.getClass().getSimpleName(), "existingUri same as defaultUri.");

///////////////////////////////////////////////////////////////////////////
// The existingUri is same as defaultUri, and showDefault is true.
// So, we check the "Default" RadioButton.
//////////////////////////////////////////////////////////////////////////
((RadioButton) findViewById(DEFAULT_RADIO_BTN_ID)).setChecked(true);
setPickedUri(defaultUri);
// pickedUri = defaultUri;

} else {

//Log.e(this.getClass().getSimpleName(), "existingUri is NOT same as defaultUri.");

// Find index of existingUri in toneUriList
int index = toneUriList.indexOf(existingUri);

if (index != - 1) {

//Log.e(this.getClass().getSimpleName(), "existingUri is available in toneUriList.");

// toneUriList has existingUri. Check the corresponding RadioButton.
((RadioButton) findViewById(toneIdList.get(index))).setChecked(true);
setPickedUri(existingUri);
//pickedUri = existingUri;

} else {
//Log.e(this.getClass().getSimpleName(), "existingUri is NOT available in toneUriList.");
///////////////////////////////////////////////////////////////////////
// toneUriList does NOT have existingUri. It is a custom Uri
// provided to us. We have to first check whether the file exists
Expand All @@ -308,49 +294,42 @@ private void populateRadioGroup() {
try (Cursor cursor = getContentResolver()
.query(existingUri, null, null, null, null)) {

if (cursor != null) {
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
// existingUri is a valid Uri.

//Log.e(this.getClass().getSimpleName(), "Custom existingUri exists.");
String fileNameWithExt;
int columnIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (columnIndex != - 1) {
fileNameWithExt = cursor.getString(columnIndex);
} else {
fileNameWithExt = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
}

int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
cursor.moveToFirst();

String fileNameWithExt = cursor.getString(nameIndex);
String fileNameWithoutExt = fileNameWithExt.substring(0,
fileNameWithExt.indexOf("."));
int toneId = View.generateViewId();

toneNameList.add(fileNameWithoutExt);
toneNameList.add(fileNameWithExt);
toneUriList.add(existingUri);
toneIdList.add(toneId);

createOneRadioButton(toneId, fileNameWithoutExt);
createOneRadioButton(toneId, fileNameWithExt);

((RadioButton) findViewById(toneId)).setChecked(true);

setPickedUri(existingUri);
//pickedUri = existingUri;

} /*else {
//Log.e(this.getClass().getSimpleName(), "Custom existingUri does NOT exist.");
}*/
}
}
}
}
} else {

//Log.e(this.getClass().getSimpleName(), "existingUri is null.");

if (wasExistingUriGiven) {
//Log.e(this.getClass().getSimpleName(), "existingUri was given.");
//////////////////////////////////////////////////////////////////////////
// existingUri was specifically passed as a null value. If showSilent
// is true, we pre-select the "Silent" RadioButton. Otherwise
// we do not select any specific RadioButton.
/////////////////////////////////////////////////////////////////////////
if (showSilent) {
//Log.e(this.getClass().getSimpleName(), "showSilent is true.");
((RadioButton) findViewById(SILENT_RADIO_BTN_ID)).setChecked(true);
}
}
Expand Down Expand Up @@ -405,17 +384,13 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
@Override
public void onClick(View view) {
if (view.getId() == DEFAULT_RADIO_BTN_ID) {
//Log.e(this.getClass().getSimpleName(), "default button clicked.");
pickedUri = defaultUri;
playChosenTone();
} else if (view.getId() == SILENT_RADIO_BTN_ID) {
//Log.e(this.getClass().getSimpleName(), "silent button clicked.");
pickedUri = null;
} else if (view.getId() == R.id.chooseCustomToneConstarintLayout) {
openFileBrowser();
} else {
/*Log.e(this.getClass().getSimpleName(),
"clicked: " + toneUriList.get(toneIdList.indexOf(view.getId())));*/
pickedUri = toneUriList.get(toneIdList.indexOf(view.getId()));
playChosenTone();
}
Expand All @@ -428,17 +403,13 @@ public void onBackPressed() {

if (pickedUri == null) {
if (showSilent) {
Intent intent = new Intent();
intent.putExtra(EXTRA_RINGTONE_PICKED_URI, pickedUri);

Intent intent = new Intent().putExtra(EXTRA_RINGTONE_PICKED_URI, pickedUri);
setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
}
} else {
Intent intent = new Intent();
intent.putExtra(EXTRA_RINGTONE_PICKED_URI, pickedUri);

Intent intent = new Intent().putExtra(EXTRA_RINGTONE_PICKED_URI, pickedUri);
setResult(RESULT_OK, intent);
}
finish();
Expand Down Expand Up @@ -476,8 +447,7 @@ private boolean isPermissionAvailable() {

private void checkAndRequestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
/////////////////////////////////////////////////////////////////
//User has denied the permission once or more than once, but
// never clicked on "Don't ask again" before denying.
Expand All @@ -489,29 +459,23 @@ private void checkAndRequestPermission() {
// 1. We are asking for the permission the first time.
// 2. User has clicked on "Don't ask again".
///////////////////////////////////////////////////////////////
if (! sharedPreferences
.getBoolean(ConstantsAndStatics.SHARED_PREF_KEY_PERMISSION_WAS_ASKED_BEFORE,
false)) {
if (! sharedPreferences.getBoolean(ConstantsAndStatics.SHARED_PREF_KEY_PERMISSION_WAS_ASKED_BEFORE, false)) {
// Permission was never asked before.
sharedPreferences.edit()
.remove(ConstantsAndStatics.SHARED_PREF_KEY_PERMISSION_WAS_ASKED_BEFORE)
.putBoolean(ConstantsAndStatics.SHARED_PREF_KEY_PERMISSION_WAS_ASKED_BEFORE, true)
.commit();

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_CODE);
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_REQUEST_CODE);

} else {
////////////////////////////////////////////////////////////////////////////////
// User had chosen "Don't ask again".
////////////////////////////////////////////////////////////////////////////////
showPermissionExplanationDialog();
}


}

}
}

Expand Down Expand Up @@ -553,8 +517,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
Uri toneUri = data.getData();
assert toneUri != null;

try (Cursor cursor = getContentResolver()
.query(toneUri, null, null, null, null)) {
try (Cursor cursor = getContentResolver().query(toneUri, null, null, null, null)) {

if (cursor != null) {

Expand All @@ -569,8 +532,7 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
cursor.moveToFirst();

String fileNameWithExt = cursor.getString(nameIndex);
String fileNameWithoutExt = fileNameWithExt
.substring(0, fileNameWithExt.indexOf("."));
String fileNameWithoutExt = fileNameWithExt.substring(0, fileNameWithExt.indexOf("."));
int toneId = View.generateViewId();

toneNameList.add(fileNameWithoutExt);
Expand Down Expand Up @@ -649,4 +611,5 @@ protected void onDestroy() {
} catch (Exception ignored) {
}
}

}
Loading

0 comments on commit 5df689d

Please sign in to comment.