/[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 22 - (show annotations) (download)
Sun Dec 11 12:59:03 2011 UTC (12 years, 3 months ago) by zoff99
File size: 6374 byte(s)
new search, disabled full-map-search, new activity for recent destinations, be less verbose
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 String[] t = new String[Navit.map_points.size()];
65 try
66 {
67 int j = 0;
68 for (j = Navit.map_points.size() - 1; j >= 0; j--)
69 {
70 t[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).point_name;
71 }
72 for (j = 0; j < t.length; j++)
73 {
74 listview_items.add(t[j]);
75 }
76 }
77 catch (Exception e)
78 {
79 e.printStackTrace();
80 t = new String[1];
81 t[0] = "* Error *";
82 listview_items.add(t[0]);
83 }
84 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listview_items);
85 this.setListAdapter(adapter);
86 this.getListView().setFastScrollEnabled(true);
87 registerForContextMenu(this.getListView());
88 my_id = this.getListView().getId();
89 }
90
91 public static Handler handler1 = new Handler()
92 {
93 public void handleMessage(Message msg)
94 {
95 if (msg.getData().getInt("what") == 1)
96 {
97 refresh_items_real();
98 }
99 }
100 };
101
102 public static void refresh_items()
103 {
104 Message msg = new Message();
105 Bundle b = new Bundle();
106 b.putInt("what", 1);
107 msg.setData(b);
108 handler1.sendMessage(msg);
109 }
110
111 public static void refresh_items_real()
112 {
113 String[] t = new String[Navit.map_points.size()];
114 ArrayAdapter<String> adapter = (ArrayAdapter<String>) my.getListAdapter();
115 listview_items.clear();
116 adapter.notifyDataSetChanged();
117 try
118 {
119 int j = 0;
120 for (j = Navit.map_points.size() - 1; j >= 0; j--)
121 {
122 t[Navit.map_points.size() - j - 1] = Navit.map_points.get(j).point_name;
123 }
124 for (j = 0; j < t.length; j++)
125 {
126 listview_items.add(t[j]);
127 }
128 }
129 catch (Exception e)
130 {
131 e.printStackTrace();
132 t = new String[1];
133 t[0] = "* Error *";
134 listview_items.add(t[0]);
135 }
136 adapter.notifyDataSetChanged();
137 refresh_items = false;
138 }
139
140 @Override
141 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
142 {
143 if (my_id != 0)
144 {
145 if (v.getId() == my_id)
146 {
147 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
148 menu.setHeaderTitle(Navit.map_points.get(Navit.map_points.size() - info.position - 1).point_name);
149 String[] menuItems = context_items;
150 for (int i = 0; i < menuItems.length; i++)
151 {
152 menu.add(Menu.NONE, i, i, menuItems[i]);
153 }
154 }
155 }
156 }
157
158 @Override
159 public boolean onContextItemSelected(MenuItem item)
160 {
161 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
162 int menuItemIndex = item.getItemId();
163 String menuItemName = context_items[menuItemIndex];
164 t_position = info.position;
165 t_size = Navit.map_points.size();
166 String listItemName = Navit.map_points.get(t_size - t_position - 1).point_name;
167
168 switch (menuItemIndex)
169 {
170 case 0:
171 // delete item
172 Navit.map_points.remove(t_size - t_position - 1);
173 // save it
174 Navit.write_map_points();
175 // refresh
176 refresh_items = true;
177 refresh_items();
178 break;
179 case 1:
180 // rename item
181 NavitRecentDestinationActivity.t = Navit.map_points.get(t_size - t_position - 1);
182 String title = Navit.get_text("Rename Destination"); //TRANS
183 RenameDialog rd = new RenameDialog(this, title, t.point_name, new RenameHandlerInterface.OnRenameItemListener()
184 {
185 @Override
186 public void onRenameItem(String newname)
187 {
188 NavitRecentDestinationActivity.t.point_name = newname;
189 Navit.map_points.set(t_size - t_position - 1, NavitRecentDestinationActivity.t);
190 System.out.println("new=" + newname);
191 // save it
192 Navit.write_map_points();
193 // refresh
194 refresh_items = true;
195 refresh_items();
196 }
197 }
198
199 );
200 rd.show();
201 break;
202 }
203 return true;
204 }
205
206 @Override
207 protected void onListItemClick(ListView l, View v, int position, long id)
208 {
209 super.onListItemClick(l, v, position, id);
210 // Get the item that was clicked
211 this.selected_id = position;
212 // close this activity
213 executeDone();
214 }
215
216 private void executeDone()
217 {
218 Intent resultIntent = new Intent();
219 resultIntent.putExtra("selected_id", String.valueOf(this.selected_id));
220 setResult(Activity.RESULT_OK, resultIntent);
221 finish();
222 }
223 }

   
Visit the ZANavi Wiki