1 /**********************************************************************
3 * Description: Android app for finding directions using the sun
4 * Author: Agnibho Mondal
5 * Website: http://code.agnibho.com/solarcompass
6 **********************************************************************
7 Copyright (c) 2016 Agnibho Mondal
9 **********************************************************************
10 This file is part of Solar Compass.
12 Solar Compass is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 Solar Compass is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with Solar Compass. If not, see <http://www.gnu.org/licenses/>.
24 **********************************************************************/
26 package com
.agnibho
.android
.solarcompass
;
28 import android
.app
.Activity
;
29 import android
.app
.AlertDialog
;
30 import android
.content
.ClipData
;
31 import android
.content
.Context
;
32 import android
.content
.DialogInterface
;
33 import android
.content
.Intent
;
34 import android
.location
.Location
;
35 import android
.location
.LocationManager
;
36 import android
.os
.Build
;
37 import android
.os
.Bundle
;
38 import android
.view
.DragEvent
;
39 import android
.view
.MotionEvent
;
40 import android
.view
.View
;
41 import android
.view
.ViewTreeObserver
;
42 import android
.view
.animation
.LinearInterpolator
;
43 import android
.view
.animation
.RotateAnimation
;
44 import android
.widget
.ImageButton
;
45 import android
.widget
.ImageView
;
46 import android
.widget
.TextView
;
47 import android
.widget
.Toast
;
49 import java
.util
.Calendar
;
51 public class MainActivity
extends Activity
{
53 private LocationData locationData
=LocationData
.getInstance();
54 private float[] center
=new float[2];
55 private float currCompass
=0;
57 private TextView displayLoc
;
58 private ImageButton locBtn
;
59 private ImageButton helpBtn
;
60 private ImageView dial
;
61 private ImageView compass
;
64 protected void onCreate(Bundle savedInstanceState
) {
65 super.onCreate(savedInstanceState
);
66 setContentView(R
.layout
.activity_main
);
68 if(Calendar
.getInstance().get(Calendar
.HOUR_OF_DAY
)<6 || Calendar
.getInstance().get(Calendar
.HOUR_OF_DAY
)>18){
72 displayLoc
=(TextView
)findViewById(R
.id
.textView4
);
75 if(locationData
.isAvailable()) {
79 startActivity(new Intent(MainActivity
.this, LocationActivity
.class));
85 locBtn
= (ImageButton
)findViewById(R
.id
.imageButton
);
86 locBtn
.setOnClickListener(new View
.OnClickListener() {
88 public void onClick(View v
) {
89 startActivity(new Intent(MainActivity
.this, LocationActivity
.class));
96 helpBtn
= (ImageButton
)findViewById(R
.id
.imageButton2
);
97 helpBtn
.setOnClickListener(new View
.OnClickListener() {
99 public void onClick(View v
) {
100 startActivity(new Intent(MainActivity
.this, HelpActivity
.class));
107 dial
=(ImageView
)findViewById(R
.id
.imageView
);
108 dial
.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver
.OnGlobalLayoutListener() {
110 public void onGlobalLayout(){
111 center
[0]=dial
.getPivotX();
112 center
[1]=dial
.getPivotY();
114 if (Build
.VERSION
.SDK_INT
>=16){
115 dial
.getViewTreeObserver().removeOnGlobalLayoutListener(this);
118 dial
.getViewTreeObserver().removeGlobalOnLayoutListener(this);
122 dial
.setOnTouchListener(new View
.OnTouchListener() {
124 public boolean onTouch(View v
, MotionEvent event
) {
125 if (event
.getAction() == MotionEvent
.ACTION_DOWN
) {
126 ClipData data
= ClipData
.newPlainText("", "");
127 View
.DragShadowBuilder shadow
= new View
.DragShadowBuilder();
128 dial
.startDrag(data
, shadow
, dial
, 0);
135 dial
.setOnDragListener(new View
.OnDragListener() {
137 public boolean onDrag(View v
, DragEvent event
) {
138 if (event
.getAction() == DragEvent
.ACTION_DRAG_LOCATION
) {
139 dial
.setRotation((float) getAngle(event
.getX(), event
.getY()));
141 else if(event
.getAction()==DragEvent
.ACTION_DROP
){
151 compass
=(ImageView
)findViewById(R
.id
.imageView2
);
155 protected void onResume(){
160 private double getAngle(float x
, float y
) {
164 angle
=Math
.toDegrees(Math
.atan2(x
,y
));
165 angle
=dial
.getRotation()+angle
;
169 private void gpsLocation(){
170 LocationManager locationManager
=(LocationManager
)getSystemService(Context
.LOCATION_SERVICE
);
172 Location loc
= locationManager
.getLastKnownLocation(LocationManager
.GPS_PROVIDER
);
174 loc
=locationManager
.getLastKnownLocation(LocationManager
.NETWORK_PROVIDER
);
176 locationData
.setCoordinate(loc
.getLatitude(), loc
.getLongitude());
178 catch (SecurityException e
){
179 Toast
.makeText(getApplicationContext(), "GPS Permission denied.", Toast
.LENGTH_LONG
).show();
182 Toast
.makeText(getApplicationContext(), "GPS location unavailable.", Toast
.LENGTH_LONG
).show();
186 private void displayLocation(){
188 if(locationData
.getLatitude()>=0){
189 s
=s
+String
.format("%.2f", Math
.abs(locationData
.getLatitude()))+"\u00B0 N \n";
192 s
=s
+String
.format("%.2f", Math
.abs(locationData
.getLatitude()))+"\u00B0 S \n";
194 if(locationData
.getLongitude()>=0){
195 s
=s
+String
.format("%.2f", Math
.abs(locationData
.getLongitude()))+"\u00B0 E";
198 s
=s
+String
.format("%.2f", Math
.abs(locationData
.getLongitude()))+"\u00B0 W";
200 displayLoc
.setText(s
);
203 private void setDirection(){
204 float direction
=(float)CompassLogic
.getAngle();
205 direction
=direction
+(dial
.getRotation()%360);
206 if(Math
.abs(direction
-currCompass
)>180){
207 direction
=(direction
-360)%360;
209 RotateAnimation anim
= new RotateAnimation(currCompass
, direction
, compass
.getPivotX(), compass
.getPivotY());
210 anim
.setInterpolator(new LinearInterpolator());
211 anim
.setDuration(500);
212 anim
.setFillAfter(true);
213 compass
.startAnimation(anim
);
214 currCompass
=direction
;
217 private void nightAlert(){
218 new AlertDialog
.Builder(this)
219 .setTitle("Warning!")
220 .setMessage("Solar Compass only works during the day.\n" +
222 "Because it depends on the location of the sun in the sky to find directions.")
223 .setNeutralButton(android
.R
.string
.ok
, new DialogInterface
.OnClickListener() {
225 public void onClick(DialogInterface dialog
, int which
) {
229 .setIcon(android
.R
.drawable
.ic_dialog_alert
)