/[zanavi_public1]/navit/navit/android/src/com/zoffcc/applications/zanavi/NavitRecentDestinationActivity.java
ZANavi

Contents of /navit/navit/android/src/com/zoffcc/applications/zanavi/NavitRecentDestinationActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 36 - (show annotations) (download)
Sat Mar 8 17:10:49 2014 UTC (10 years, 1 month ago) by zoff99
File size: 9179 byte(s)
new market version, lots of new features
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2011 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 package com.zoffcc.applications.zanavi;
21
22 import java.util.ArrayList;
23
24 import android.app.Activity;
25 import android.app.ListActivity;
26 import android.content.Intent;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Message;
30 import android.view.ContextMenu;
31 import android.view.ContextMenu.ContextMenuInfo;
32 import android.view.Menu;
33 import android.view.MenuItem;
34 import android.view.View;
35 import android.widget.AdapterView;
36 import android.widget.ListView;
37
38 import com.retain.dialog.RenameDialog;
39 import com.retain.dialog.RenameHandlerInterface;
40 import com.zoffcc.applications.zanavi.Navit.Navit_Point_on_Map;
41
42 public class NavitRecentDestinationActivity extends ListActivity
43 {
44 private int selected_id = -1;
45 private int my_id = 0;
46 private String[] context_items = null;
47 static Navit_Point_on_Map t = null;
48 static int t_position = -1;
49 static int t_size = -1;
50 static Boolean refresh_items = false;
51 static NavitRecentDestinationActivity my = null;
52 private static ArrayList<String> listview_items = new ArrayList<String>();
53 private static ArrayList<String> listview_addons = new ArrayList<String>();
54
55 @Override
56 public void onCreate(Bundle savedInstanceState)
57 {
58 super.onCreate(savedInstanceState);
59 my = this;
60
61 context_items = new String[] { Navit.get_text("delete Destination"), Navit.get_text("rename Destination"), Navit.get_text("set as Home Location") }; // TRANS
62
63 listview_items.clear();
64 listview_addons.clear();
65
66 // crash reported on google play store
67 // gueard against nullpointer
68 if (Navit.map_points == null)
69 {
70 Navit.map_points = new ArrayList<Navit_Point_on_Map>();
71 }
72 // crash reported on google play store
73 // guard against nullpointer
74
75 String[] t = new String[Navit.map_points.size()];
76 String[] t_addons = new String[Navit.map_points.size()];
77 try
78 {
79 int j = 0;
80 for (j = Navit.map_points.size() - 1; j >= 0; j--)
81 {
82 t[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).point_name;
83 t_addons[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).addon;
84 }
85
86 for (j = 0; j < t.length; j++)
87 {
88 listview_items.add(t[j]);
89 listview_addons.add(t_addons[j]);
90 }
91 }
92 catch (Exception e)
93 {
94 e.printStackTrace();
95 t = new String[1];
96 t[0] = "* Error *";
97 listview_items.add(t[0]);
98 listview_addons.add(null);
99 }
100 NavitArrayAdapter adapter = new NavitArrayAdapter(this, listview_items, listview_addons);
101
102 //if (convertView == null)
103 //{
104 // LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
105 // convertView = inflater.inflate(R.layout.search_result_list_header, null);
106 //}
107
108 this.setListAdapter(adapter);
109 this.getListView().setFastScrollEnabled(true);
110 registerForContextMenu(this.getListView());
111 my_id = this.getListView().getId();
112 }
113
114 public static Handler handler1 = new Handler()
115 {
116 public void handleMessage(Message msg)
117 {
118 if (msg.getData().getInt("what") == 1)
119 {
120 int i = msg.getData().getInt("i");
121 refresh_items_real(i);
122 }
123 }
124 };
125
126 public static void refresh_items(int i)
127 {
128 Message msg = new Message();
129 Bundle b = new Bundle();
130 b.putInt("what", 1);
131 b.putInt("i", i);
132 msg.setData(b);
133 handler1.sendMessage(msg);
134 }
135
136 public static void refresh_items_real(int i)
137 {
138 String[] t = new String[Navit.map_points.size()];
139 String[] t_addons = new String[Navit.map_points.size()];
140 NavitArrayAdapter adapter = (NavitArrayAdapter) my.getListAdapter();
141 listview_items.clear();
142 listview_addons.clear();
143 adapter.notifyDataSetChanged();
144 try
145 {
146 int j = 0;
147 for (j = Navit.map_points.size() - 1; j >= 0; j--)
148 {
149 t[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).point_name;
150 t_addons[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).addon;
151 // System.out.println("name=" + Navit.map_points.get(j).point_name + " addon=" + Navit.map_points.get(j).addon);
152 }
153 for (j = 0; j < t.length; j++)
154 {
155 listview_items.add(t[j]);
156 listview_addons.add(t_addons[j]);
157 }
158 }
159 catch (Exception e)
160 {
161 e.printStackTrace();
162 t = new String[1];
163 t[0] = "* Error *";
164 listview_items.add(t[0]);
165 listview_addons.add(null);
166 }
167 adapter.notifyDataSetChanged();
168 refresh_items = false;
169 }
170
171 @Override
172 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
173 {
174 if (my_id != 0)
175 {
176 if (v.getId() == my_id)
177 {
178 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
179 menu.setHeaderTitle(Navit.map_points.get(Navit.map_points.size() - info.position - 1).point_name);
180 String[] menuItems = context_items;
181 for (int i = 0; i < menuItems.length; i++)
182 {
183 menu.add(Menu.NONE, i, i, menuItems[i]);
184 }
185 }
186 }
187 }
188
189 @Override
190 public boolean onContextItemSelected(MenuItem item)
191 {
192 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
193 int menuItemIndex = item.getItemId();
194 String menuItemName = context_items[menuItemIndex];
195 t_position = info.position;
196 t_size = Navit.map_points.size();
197 String listItemName = Navit.map_points.get(t_size - t_position - 1).point_name;
198
199 switch (menuItemIndex)
200 {
201 case 0:
202 // delete item
203 Navit.map_points.remove(t_size - t_position - 1);
204 // save it
205 Navit.write_map_points();
206 // refresh
207 refresh_items = true;
208 refresh_items(1);
209 break;
210 case 1:
211 // rename item
212 NavitRecentDestinationActivity.t = Navit.map_points.get(t_size - t_position - 1);
213 String title = Navit.get_text("Rename Destination"); //TRANS
214 RenameDialog rd = new RenameDialog(this, title, t.point_name, new RenameHandlerInterface.OnRenameItemListener()
215 {
216 @Override
217 public void onRenameItem(String newname)
218 {
219 NavitRecentDestinationActivity.t.point_name = newname;
220 Navit.map_points.set(t_size - t_position - 1, NavitRecentDestinationActivity.t);
221 System.out.println("new=" + newname);
222 // save it
223 Navit.write_map_points();
224 // refresh
225 refresh_items = true;
226 refresh_items(0);
227 }
228 }
229
230 );
231 rd.show();
232 break;
233 case 2:
234 // find old HOME item
235 int old_home_id = Navit.find_home_point();
236 if (old_home_id != -1)
237 {
238 NavitRecentDestinationActivity.t = Navit.map_points.get(old_home_id);
239 NavitRecentDestinationActivity.t.addon = null;
240 Navit.map_points.set(old_home_id, NavitRecentDestinationActivity.t);
241 }
242 // in case of double home, because of some strange error
243 old_home_id = Navit.find_home_point();
244 if (old_home_id != -1)
245 {
246 NavitRecentDestinationActivity.t = Navit.map_points.get(old_home_id);
247 NavitRecentDestinationActivity.t.addon = null;
248 Navit.map_points.set(old_home_id, NavitRecentDestinationActivity.t);
249 }
250 // in case of double home, because of some strange error
251 old_home_id = Navit.find_home_point();
252 if (old_home_id != -1)
253 {
254 NavitRecentDestinationActivity.t = Navit.map_points.get(old_home_id);
255 NavitRecentDestinationActivity.t.addon = null;
256 Navit.map_points.set(old_home_id, NavitRecentDestinationActivity.t);
257 }
258
259 // set HOME item
260 NavitRecentDestinationActivity.t = Navit.map_points.get(t_size - t_position - 1);
261 NavitRecentDestinationActivity.t.addon = "1";
262 // delete item
263 Navit.map_points.remove(t_size - t_position - 1);
264 // add it to the first position
265 Navit.map_points.add(NavitRecentDestinationActivity.t);
266 // save it
267 Navit.write_map_points();
268 // refresh
269 refresh_items = true;
270 refresh_items(0);
271 break;
272 }
273 return true;
274 }
275
276 @Override
277 protected void onListItemClick(ListView l, View v, int position, long id)
278 {
279 super.onListItemClick(l, v, position, id);
280 // Get the item that was clicked
281
282 int t_p = position;
283 int t_s = Navit.map_points.size();
284 // compensate "selected_id" for reverse listing order of items!
285 this.selected_id = t_s - t_p - 1;
286 // close this activity
287 executeDone();
288 }
289
290 private void executeDone()
291 {
292 Intent resultIntent = new Intent();
293 resultIntent.putExtra("selected_id", String.valueOf(this.selected_id));
294 setResult(Activity.RESULT_OK, resultIntent);
295 finish();
296 }
297 }

   
Visit the ZANavi Wiki