/[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 31 - (show annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (11 years, 1 month ago) by zoff99
File size: 6766 byte(s)
new map version, lots of fixes and experimental 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.ArrayAdapter;
37 import android.widget.ListView;
38
39 import com.retain.dialog.RenameDialog;
40 import com.retain.dialog.RenameHandlerInterface;
41 import com.zoffcc.applications.zanavi.Navit.Navit_Point_on_Map;
42
43 public class NavitRecentDestinationActivity extends ListActivity
44 {
45 private int selected_id = -1;
46 private int my_id = 0;
47 private String[] context_items = null;
48 static Navit_Point_on_Map t = null;
49 static int t_position = -1;
50 static int t_size = -1;
51 static Boolean refresh_items = false;
52 static NavitRecentDestinationActivity my = null;
53 private static ArrayList<String> listview_items = 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") }; // TRANS
62
63 listview_items.clear();
64
65 // crash reported on google play store
66 // gueard against nullpointer
67 if (Navit.map_points == null)
68 {
69 Navit.map_points = new ArrayList<Navit_Point_on_Map>();
70 }
71 // crash reported on google play store
72 // gueard against nullpointer
73
74 String[] t = new String[Navit.map_points.size()];
75 try
76 {
77 int j = 0;
78 for (j = Navit.map_points.size() - 1; j >= 0; j--)
79 {
80 t[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).point_name;
81 }
82 for (j = 0; j < t.length; j++)
83 {
84 listview_items.add(t[j]);
85 }
86 }
87 catch (Exception e)
88 {
89 e.printStackTrace();
90 t = new String[1];
91 t[0] = "* Error *";
92 listview_items.add(t[0]);
93 }
94 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listview_items);
95 this.setListAdapter(adapter);
96 this.getListView().setFastScrollEnabled(true);
97 registerForContextMenu(this.getListView());
98 my_id = this.getListView().getId();
99 }
100
101 public static Handler handler1 = new Handler()
102 {
103 public void handleMessage(Message msg)
104 {
105 if (msg.getData().getInt("what") == 1)
106 {
107 refresh_items_real();
108 }
109 }
110 };
111
112 public static void refresh_items()
113 {
114 Message msg = new Message();
115 Bundle b = new Bundle();
116 b.putInt("what", 1);
117 msg.setData(b);
118 handler1.sendMessage(msg);
119 }
120
121 public static void refresh_items_real()
122 {
123 String[] t = new String[Navit.map_points.size()];
124 ArrayAdapter<String> adapter = (ArrayAdapter<String>) my.getListAdapter();
125 listview_items.clear();
126 adapter.notifyDataSetChanged();
127 try
128 {
129 int j = 0;
130 for (j = Navit.map_points.size() - 1; j >= 0; j--)
131 {
132 t[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).point_name;
133 }
134 for (j = 0; j < t.length; j++)
135 {
136 listview_items.add(t[j]);
137 }
138 }
139 catch (Exception e)
140 {
141 e.printStackTrace();
142 t = new String[1];
143 t[0] = "* Error *";
144 listview_items.add(t[0]);
145 }
146 adapter.notifyDataSetChanged();
147 refresh_items = false;
148 }
149
150 @Override
151 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
152 {
153 if (my_id != 0)
154 {
155 if (v.getId() == my_id)
156 {
157 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
158 menu.setHeaderTitle(Navit.map_points.get(Navit.map_points.size() - info.position - 1).point_name);
159 String[] menuItems = context_items;
160 for (int i = 0; i < menuItems.length; i++)
161 {
162 menu.add(Menu.NONE, i, i, menuItems[i]);
163 }
164 }
165 }
166 }
167
168 @Override
169 public boolean onContextItemSelected(MenuItem item)
170 {
171 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
172 int menuItemIndex = item.getItemId();
173 String menuItemName = context_items[menuItemIndex];
174 t_position = info.position;
175 t_size = Navit.map_points.size();
176 String listItemName = Navit.map_points.get(t_size - t_position - 1).point_name;
177
178 switch (menuItemIndex)
179 {
180 case 0:
181 // delete item
182 Navit.map_points.remove(t_size - t_position - 1);
183 // save it
184 Navit.write_map_points();
185 // refresh
186 refresh_items = true;
187 refresh_items();
188 break;
189 case 1:
190 // rename item
191 NavitRecentDestinationActivity.t = Navit.map_points.get(t_size - t_position - 1);
192 String title = Navit.get_text("Rename Destination"); //TRANS
193 RenameDialog rd = new RenameDialog(this, title, t.point_name, new RenameHandlerInterface.OnRenameItemListener()
194 {
195 @Override
196 public void onRenameItem(String newname)
197 {
198 NavitRecentDestinationActivity.t.point_name = newname;
199 Navit.map_points.set(t_size - t_position - 1, NavitRecentDestinationActivity.t);
200 System.out.println("new=" + newname);
201 // save it
202 Navit.write_map_points();
203 // refresh
204 refresh_items = true;
205 refresh_items();
206 }
207 }
208
209 );
210 rd.show();
211 break;
212 }
213 return true;
214 }
215
216 @Override
217 protected void onListItemClick(ListView l, View v, int position, long id)
218 {
219 super.onListItemClick(l, v, position, id);
220 // Get the item that was clicked
221
222 int t_p = position;
223 int t_s = Navit.map_points.size();
224 // compensate "selected_id" for reverse listing order of items!
225 this.selected_id = t_s - t_p - 1;
226 // close this activity
227 executeDone();
228 }
229
230 private void executeDone()
231 {
232 Intent resultIntent = new Intent();
233 resultIntent.putExtra("selected_id", String.valueOf(this.selected_id));
234 setResult(Activity.RESULT_OK, resultIntent);
235 finish();
236 }
237 }

   
Visit the ZANavi Wiki