Skip to content

Commit

Permalink
Initial attempt at adding Qibla direction and exact alarm fix for and…
Browse files Browse the repository at this point in the history
…roid 32
  • Loading branch information
hani-28 committed Feb 16, 2022
1 parent ff704f5 commit 956f9cc
Show file tree
Hide file tree
Showing 23 changed files with 573 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ android {
applicationId "com.HMSolutions.thikrallah"
minSdkVersion 21
targetSdkVersion 31
versionCode 120
versionName "6.0.0"
versionCode 121
versionName "6.1.0"
}
lintOptions {
disable 'MissingTranslation'
Expand Down Expand Up @@ -62,10 +62,10 @@ repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://maven.google.com" }


}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.HMSolutions.thikrallah.hisnulmuslim.DuaGroupActivity;
import com.HMSolutions.thikrallah.quran.labs.androidquran.QuranDataActivity;


import java.util.Locale;

public class MainFragment extends Fragment {
Expand Down Expand Up @@ -69,6 +70,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
Button button_quran = (Button) view.findViewById(R.id.button_quran);
Button button_hisn_almuslim = (Button) view.findViewById(R.id.hisn_almuslim);
Button button_athan = (Button) view.findViewById(R.id.button_athan);
Button button_qibla = (Button) view.findViewById(R.id.button_qibla);

button_athan.setOnClickListener(new OnClickListener() {

@Override
Expand All @@ -77,6 +80,16 @@ public void onClick(View v) {

}

});
button_qibla.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
mCallback.launchFragment(new QiblaFragment(), new Bundle(), "QiblaFragment");


}

});
mPrefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
package com.HMSolutions.thikrallah.Fragments;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;

import com.HMSolutions.thikrallah.MainActivity;
import com.HMSolutions.thikrallah.R;
import com.HMSolutions.thikrallah.Utilities.CustomLocation;
import com.HMSolutions.thikrallah.Utilities.MainInterface;
import com.HMSolutions.thikrallah.compass.Compass;
import com.HMSolutions.thikrallah.compass.SOTWFormatter;



public class QiblaFragment extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener, View.OnClickListener {
private MainInterface mCallback;
private SharedPreferences.OnSharedPreferenceChangeListener prefListener;
private CheckBox is_Manual_Location;
private TextView currentLocation;
private static final String TAG = "CompassActivity";
private Compass compass;
private ImageView arrowView;
private ImageView dialView;
private TextView sotwLabel; // SOTW is for "side of the world"
private Context mContext;
private float currentAzimuth;
private float currentQibla;
private SOTWFormatter sotwFormatter;

// private TextView locationDescription;


public QiblaFragment() {
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equalsIgnoreCase("latitude") || key.equalsIgnoreCase("longitude")
|| key.equalsIgnoreCase("isCustomLocation")||key.equalsIgnoreCase("c_latitude")
||key.equalsIgnoreCase("c_longitude")) {
if (this.getView()!=null){
updateQiblaDirection();
is_Manual_Location.setChecked(PreferenceManager.getDefaultSharedPreferences(this.getContext()).getBoolean("isCustomLocation",false));
}

}

}

private void updateQiblaDirection() {
this.calculateQiblaDirection();
currentLocation.setText(this.getContext().getResources().getString(R.string.current_location)
+MainActivity.getLatitude(getContext())+"; "+ MainActivity.getLongitude(getContext()));
}



@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext=context;
MainActivity.setLocale(context);
try {
prefListener = this;
mCallback = (MainInterface) context;
mCallback.requestLocationUpdate();


} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement MainInterface");
}
Log.d(TAG, "start compass");


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContext=this.getActivity();
MainActivity.setLocale(this.getContext());
((AppCompatActivity) this.getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) this.getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
this.setHasOptionsMenu(true);

View view = inflater.inflate(R.layout.compass_fragment, container,
false);

sotwFormatter = new SOTWFormatter(this.getActivity().getApplicationContext());

arrowView = view.findViewById(R.id.main_image_hands);
dialView = view.findViewById(R.id.main_image_dial);
setupCompass();
//locationDescription=(TextView) view.findViewById(R.id.textView_location);

is_Manual_Location= (CheckBox) view.findViewById(R.id.is_manual_location);
if (PreferenceManager.getDefaultSharedPreferences(this.getContext()).getBoolean("isCustomLocation", false)) {
is_Manual_Location.setChecked(true);
}else{
is_Manual_Location.setChecked(false);
}

is_Manual_Location.setOnClickListener(this);
currentLocation= view.findViewById(R.id.current_location);
this.updateQiblaDirection();
PreferenceManager.getDefaultSharedPreferences(this.getContext()).registerOnSharedPreferenceChangeListener(prefListener);
return view;
}

private void setupCompass() {
compass = new Compass(this.getActivity().getApplicationContext());
Compass.CompassListener cl = getCompassListener();
compass.setListener(cl);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==android.R.id.home) {
// Respond to the action bar's Up/Home button
this.getActivity().onBackPressed();
return true;
}
return false;
}
private Compass.CompassListener getCompassListener() {
return new Compass.CompassListener() {
@Override
public void onNewAzimuth(final float azimuth) {
// UI updates only in UI thread
// https://stackoverflow.com/q/11140285/444966

((MainActivity)mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
adjustArrow(azimuth);
adjustSotwLabel(azimuth);
}
});
}
};
}

private void adjustArrow(float azimuth) {
Animation an = new RotateAnimation(-currentAzimuth, -azimuth,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
float newQibla=(float)this.calculateQiblaDirection();
Animation an2 = new RotateAnimation(currentQibla-currentAzimuth, newQibla-azimuth,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);

currentQibla=newQibla;
currentAzimuth = azimuth;

an.setDuration(500);
an.setRepeatCount(0);
an.setFillAfter(true);
dialView.startAnimation(an);


an2.setDuration(500);
an2.setRepeatCount(0);
an2.setFillAfter(true);

arrowView.startAnimation(an2);
}

private void adjustSotwLabel(float azimuth) {
sotwLabel.setText(sotwFormatter.format(azimuth));
}

@Override
public void onPause(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getActivity().getApplicationContext());
prefs.unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
compass.stop();
}

@Override
public void onResume() {
super.onResume();
PreferenceManager.getDefaultSharedPreferences(this.getContext()).registerOnSharedPreferenceChangeListener(prefListener);
logScreen();
this.updateQiblaDirection();
Log.d(TAG, "start compass");
compass.start();
}

private void logScreen() {
/*
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.SCREEN_NAME, this.getClass().getSimpleName());
bundle.putString(FirebaseAnalytics.Param.SCREEN_CLASS, this.getClass().getSimpleName());
FirebaseAnalytics.getInstance(this.getActivity()).logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, bundle);
*/
}

@Override
public void onDestroy() {
super.onDestroy();
}


@Override
public void onClick(View v) {
if (is_Manual_Location.isChecked()){
CustomLocation Customlocation=new CustomLocation(this.getActivity());
Customlocation.show();
}else{
PreferenceManager.getDefaultSharedPreferences(this.getContext()).edit().putBoolean("isCustomLocation", false).commit();
}
this.updateQiblaDirection();
}

/**
* qibla direction in degrees from the north (clock-wise)
* @return number - 0 means north, 90 means east, 270 means west, etc
*
*/
public double calculateQiblaDirection(){
double latitude = Double.parseDouble(MainActivity.getLatitude(getContext()));
double longitude = Double.parseDouble(MainActivity.getLongitude(getContext()));
double lng_a = 39.82616111;
double lat_a = 21.42250833;
double deg = Math.toDegrees(Math.atan2(Math.sin(Math.toRadians(lng_a-longitude)),
Math.cos(Math.toRadians(latitude))*Math.tan(Math.toRadians(lat_a))
-Math.sin(Math.toRadians(latitude))*Math.cos(Math.toRadians(lng_a-longitude))));
if (deg>=0){
return deg;
}else{
return deg+360;
}
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/HMSolutions/thikrallah/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ private void requestPermissions(){
Log.d(TAG,"forground_service permission requested");
}
}

int alarmsPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.SCHEDULE_EXACT_ALARM);

int mediacontrolPermission = ContextCompat.checkSelfPermission(this,
Manifest.permission.MEDIA_CONTENT_CONTROL);
Expand All @@ -390,6 +391,9 @@ private void requestPermissions(){
if (mediacontrolPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.MEDIA_CONTENT_CONTROL);
}
if (alarmsPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SCHEDULE_EXACT_ALARM);
}
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);

Expand Down Expand Up @@ -431,10 +435,6 @@ private void requestPermissions(){
new String[]{Manifest.permission.MEDIA_CONTENT_CONTROL},
1);
}




}

@Override
Expand Down
Loading

0 comments on commit 956f9cc

Please sign in to comment.