]> Softwares of Agnibho - solarcompass.git/blob - solarcompass/src/main/java/com/agnibho/android/solarcompass/MainActivity.java
Initial commit
[solarcompass.git] / solarcompass / src / main / java / com / agnibho / android / solarcompass / MainActivity.java
1 /**********************************************************************
2 * Title: Solar Compass
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
8 All rights reserved
9 **********************************************************************
10 This file is part of Solar Compass.
11
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.
16
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.
21
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 **********************************************************************/
25
26 package com.agnibho.android.solarcompass;
27
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;
48
49 import java.util.Calendar;
50
51 public class MainActivity extends Activity {
52
53 private LocationData locationData=LocationData.getInstance();
54 private float[] center=new float[2];
55 private float currCompass=0;
56
57 private TextView displayLoc;
58 private ImageButton locBtn;
59 private ImageButton helpBtn;
60 private ImageView dial;
61 private ImageView compass;
62
63 @Override
64 protected void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
66 setContentView(R.layout.activity_main);
67
68 if(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)<6 || Calendar.getInstance().get(Calendar.HOUR_OF_DAY)>18){
69 nightAlert();
70 }
71
72 displayLoc=(TextView)findViewById(R.id.textView4);
73
74 gpsLocation();
75 if(locationData.isAvailable()) {
76 displayLocation();
77 }
78 else{
79 startActivity(new Intent(MainActivity.this, LocationActivity.class));
80 }
81
82 /**
83 * LocationData Button
84 */
85 locBtn = (ImageButton)findViewById(R.id.imageButton);
86 locBtn.setOnClickListener(new View.OnClickListener() {
87 @Override
88 public void onClick(View v) {
89 startActivity(new Intent(MainActivity.this, LocationActivity.class));
90 }
91 });
92
93 /**
94 * Help Button
95 */
96 helpBtn = (ImageButton)findViewById(R.id.imageButton2);
97 helpBtn.setOnClickListener(new View.OnClickListener() {
98 @Override
99 public void onClick(View v) {
100 startActivity(new Intent(MainActivity.this, HelpActivity.class));
101 }
102 });
103
104 /**
105 * Dialer
106 */
107 dial=(ImageView)findViewById(R.id.imageView);
108 dial.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
109 @Override
110 public void onGlobalLayout(){
111 center[0]=dial.getPivotX();
112 center[1]=dial.getPivotY();
113 setDirection();
114 if (Build.VERSION.SDK_INT>=16){
115 dial.getViewTreeObserver().removeOnGlobalLayoutListener(this);
116 }
117 else {
118 dial.getViewTreeObserver().removeGlobalOnLayoutListener(this);
119 }
120 }
121 });
122 dial.setOnTouchListener(new View.OnTouchListener() {
123 @Override
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);
129 return true;
130 } else {
131 return false;
132 }
133 }
134 });
135 dial.setOnDragListener(new View.OnDragListener() {
136 @Override
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()));
140 }
141 else if(event.getAction()==DragEvent.ACTION_DROP){
142 setDirection();
143 }
144 return true;
145 }
146 });
147
148 /**
149 * Compass
150 */
151 compass=(ImageView)findViewById(R.id.imageView2);
152 }
153
154 @Override
155 protected void onResume(){
156 super.onResume();
157 displayLocation();
158 }
159
160 private double getAngle(float x, float y) {
161 double angle;
162 x=x-center[0];
163 y=center[1]-y;
164 angle=Math.toDegrees(Math.atan2(x,y));
165 angle=dial.getRotation()+angle;
166 return angle;
167 }
168
169 private void gpsLocation(){
170 LocationManager locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
171 try {
172 Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
173 if(loc==null){
174 loc=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
175 }
176 locationData.setCoordinate(loc.getLatitude(), loc.getLongitude());
177 }
178 catch (SecurityException e){
179 Toast.makeText(getApplicationContext(), "GPS Permission denied.", Toast.LENGTH_LONG).show();
180 }
181 catch (Exception e){
182 Toast.makeText(getApplicationContext(), "GPS location unavailable.", Toast.LENGTH_LONG).show();
183 }
184 }
185
186 private void displayLocation(){
187 String s="";
188 if(locationData.getLatitude()>=0){
189 s=s+String.format("%.2f", Math.abs(locationData.getLatitude()))+"\u00B0 N \n";
190 }
191 else{
192 s=s+String.format("%.2f", Math.abs(locationData.getLatitude()))+"\u00B0 S \n";
193 }
194 if(locationData.getLongitude()>=0){
195 s=s+String.format("%.2f", Math.abs(locationData.getLongitude()))+"\u00B0 E";
196 }
197 else{
198 s=s+String.format("%.2f", Math.abs(locationData.getLongitude()))+"\u00B0 W";
199 }
200 displayLoc.setText(s);
201 }
202
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;
208 }
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;
215 }
216
217 private void nightAlert(){
218 new AlertDialog.Builder(this)
219 .setTitle("Warning!")
220 .setMessage("Solar Compass only works during the day.\n" +
221 "(6 AM to 6 PM)\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() {
224 @Override
225 public void onClick(DialogInterface dialog, int which) {
226
227 }
228 })
229 .setIcon(android.R.drawable.ic_dialog_alert)
230 .show();
231 }
232 }