· 8 years ago · Jan 25, 2018, 03:00 PM
1package com.example.shaharblank.sweetrip;
2
3import android.Manifest;
4import android.content.Intent;
5import android.content.pm.PackageManager;
6import android.database.Cursor;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteException;
9import android.location.Location;
10import android.location.LocationListener;
11import android.location.LocationManager;
12import android.os.Build;
13import android.provider.Settings;
14import android.support.annotation.NonNull;
15import android.support.v4.app.ActivityCompat;
16import android.support.v7.app.AppCompatActivity;
17import android.os.Bundle;
18import android.util.Log;
19import android.view.View;
20import android.widget.ArrayAdapter;
21import android.widget.ListView;
22import android.widget.TextView;
23import android.widget.Toast;
24
25import java.util.ArrayList;
26
27public class ResturantMenu extends AppCompatActivity {
28
29 private final String DB_NAME = "ResturantsDataBase";
30 private final String TABLE_NAME = "Resturants";
31 SQLiteDatabase ResturantsDB = null;
32 private LocationManager locationManager;
33 private LocationListener locationListener;
34 private boolean isOver = false;
35
36 @Override
37 protected void onCreate(Bundle savedInstanceState) {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.activity_resturant_menu);
40
41 final ArrayList<String> Resturants = new ArrayList<String>();
42 final ListView ResturantslistView = (ListView) findViewById(R.id.ResturantListView);
43
44 try {
45 ResturantsDB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
46
47 // ResturantsDB.execSQL("DROP TABLE IF EXITS "+TABLE_NAME+";");
48 // ResturantsDB.execSQL("DELETE FROM " + TABLE_NAME);
49 ResturantsDB.execSQL("CREATE TABLE IF NOT EXISTS " +
50 TABLE_NAME +
51 " (ResturantName VARCHAR," +
52 " X DOUBLE, Y DOUBLE);");
53
54 mFillDbsTable();
55
56 Cursor c = ResturantsDB.rawQuery("SELECT * FROM " +
57 TABLE_NAME + "", null);
58
59 if (c != null) {
60 if (c.moveToFirst()) {
61 do {
62 String ResturantName = c.getString(c.getColumnIndex("ResturantName"));
63 Double X = c.getDouble(c.getColumnIndex("X"));
64 Double Y = c.getDouble(c.getColumnIndex("Y"));
65 } while (c.moveToNext());
66 }
67 }
68 //setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
69 //ContactAdapter adapter = new ContactAdapter(this,contacts);
70 /* ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, Resturants);
71 ResturantslistView.setAdapter(adapter);*/
72
73 } catch (SQLiteException se) {
74 Log.e(getClass().getSimpleName(), se.toString());
75
76 } finally {
77 if (ResturantsDB != null)
78 //ResturantsDB.execSQL("DELETE FROM " + TABLE_NAME);
79 ResturantsDB.close();
80 }
81
82
83 //to show the distance from to another location in meter
84
85 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
86 locationListener = new LocationListener() {
87 @Override
88 public void onLocationChanged(Location location) {
89 //location is the location of user
90
91 if(!isOver) {
92 try {
93 ResturantsDB = ResturantMenu.this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
94 Cursor c = ResturantsDB.rawQuery("SELECT * FROM " +
95 TABLE_NAME + "", null);
96 if (c != null) {
97 if (c.moveToFirst()) {
98 do {
99 String ResturantName = c.getString(c.getColumnIndex("ResturantName"));
100 Double X = c.getDouble(c.getColumnIndex("X"));
101 Double Y = c.getDouble(c.getColumnIndex("Y"));
102 Location Resturant = new Location("Resturant");
103 Resturant.setLatitude(X);
104 Resturant.setLongitude(Y);
105 float Distance = location.distanceTo(Resturant) / 1000;
106 int dis = (int) Distance;
107 double fraqtion = (Distance - dis) * 100;
108 int Fraq = (int) fraqtion;
109 double sumDistance = dis + ((double) Fraq / 100);
110
111 Resturants.add(ResturantName + ", " + sumDistance + " ×§×™×œ×•×ž×˜×¨×™× ×ž×ž×š ");
112 } while (c.moveToNext());
113 }
114 }
115 } catch (SQLiteException se) {
116 Log.e(getClass().getSimpleName(), se.toString());
117 }
118
119 ArrayAdapter<String> adapter = new ArrayAdapter<String>(ResturantMenu.this, android.R.layout.simple_expandable_list_item_1, Resturants);
120 ResturantslistView.setAdapter(adapter);
121 isOver = true;
122 }
123
124 }
125
126 @Override
127 public void onStatusChanged(String provider, int status, Bundle extras) {
128 //×ין צורך ×‘×¤×•× ×§×¦×™×” ×–×ת ×בל ×œ× ×œ×ž×—×•×§
129 }
130
131 @Override
132 public void onProviderEnabled(String provider) {
133 //×ין צורך ×‘×¤×•× ×§×¦×™×” ×–×ת ×בל ×œ× ×œ×ž×—×•×§
134 }
135
136 @Override
137 public void onProviderDisabled(String provider) {
138 //במקרה ש×ין גישה שולח להגדרות על ×ž× ×ª לתת גישה
139 Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
140 startActivity(intent);
141 }
142 };
143 //בודק ×× ×™×© הרש××” בשביל להשתמש במיקו×
144 //×× ×”×ª× ××™ × ×›×•×Ÿ ××– ×–×” מבקש הרש××”
145 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
146 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
147 requestPermissions(new String[]{
148 Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
149 Manifest.permission.INTERNET
150 },10);
151 }
152 return;
153 }else{
154 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener);//×¢× locationManager ×פשר לקחת ×ž×™×§×•× ×©×œ משתמש
155 }
156 }
157
158
159 private void mFillDbsTable() {
160 try {
161 ResturantsDB.execSQL("INSERT INTO " +
162 TABLE_NAME +
163 " Values ('× ×פיס',31.2426297, 34.81063130000007);");
164 ResturantsDB.execSQL("INSERT INTO " +
165 TABLE_NAME +
166 " Values ('×ž×§×¡×™×§× ×™',31.2378975, 34.79155549999996);");
167 ResturantsDB.execSQL("INSERT INTO " +
168 TABLE_NAME +
169 " Values ('×רומה',31.2454,34.78224);");
170
171 } catch (SQLiteException se) {
172 Log.e(getClass().getSimpleName(), "Could not create records");
173 }
174 }
175
176 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
177 switch (requestCode){
178 case 10:
179 if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
180 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 0, locationListener); //×מור להיות ××“×•× ×”×›×œ בסדר
181 return;
182 }
183 }
184}