Add:Android:Callback on activity change
[navit-package] / navit / android / src / org / navitproject / navit / Navit.java
1 /**
2  * Navit, a modular navigation system.
3  * Copyright (C) 2005-2008 Navit Team
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 org.navitproject.navit;
21
22 import android.app.Activity;
23 import android.widget.TextView;
24 import android.os.Bundle;
25 import android.os.Debug;
26 import android.os.Message;
27 import android.os.Handler;
28 import android.os.PowerManager;
29 import android.content.Context;
30 import android.content.res.Resources;
31 import android.util.Log;
32 import java.util.Locale;
33 import java.io.File;
34 import java.io.FileInputStream;
35 import java.io.FileOutputStream;
36 import java.io.InputStream;
37
38
39 public class Navit extends Activity implements Handler.Callback
40 {
41     public Handler handler;
42     private PowerManager.WakeLock wl;
43     private boolean extractRes(String resname, String result)
44     {
45         int slash=-1;
46         boolean needs_update=false;
47         File resultfile;
48         Resources res=getResources();
49         Log.e("Navit","Res Name " + resname);
50         Log.e("Navit","result " + result);
51         int id=res.getIdentifier(resname,"raw","org.navitproject.navit");
52         Log.e("Navit","Res ID " + id);
53         if (id == 0)
54                 return false;
55         while ((slash=result.indexOf("/",slash+1)) != -1) {
56                 if (slash != 0) {
57                         Log.e("Navit","Checking "+result.substring(0,slash));
58                         resultfile=new File(result.substring(0,slash));
59                         if (!resultfile.exists()) {
60                                 Log.e("Navit","Creating dir");
61                                 if (!resultfile.mkdir())
62                                         return false;
63                                 needs_update=true;
64                         }
65                 }
66         }
67         resultfile=new File(result);
68         if (!resultfile.exists())
69                 needs_update=true;
70         if (!needs_update) {
71                 try {
72                         InputStream resourcestream=res.openRawResource(id);
73                         FileInputStream resultfilestream=new FileInputStream(resultfile);
74                         byte[] resourcebuf = new byte[1024];
75                         byte[] resultbuf = new byte[1024];
76                         int i = 0;
77                         while ((i = resourcestream.read(resourcebuf)) != -1) {
78                                 if (resultfilestream.read(resultbuf) != i) {
79                                         Log.e("Navit","Result is too short");
80                                         needs_update=true;
81                                         break;
82                                 }
83                                 for (int j = 0 ; j < i ; j++) {
84                                         if (resourcebuf[j] != resultbuf[j]) {
85                                                 Log.e("Navit","Result is different");
86                                                 needs_update=true;
87                                                 break;
88                                         }
89                                 }
90                                 if (needs_update)
91                                         break;
92                         }
93                         if (!needs_update && resultfilestream.read(resultbuf) != -1) {
94                                 Log.e("Navit","Result is too long");
95                                 needs_update=true;
96                         }
97                 } 
98                 catch (Exception e) {
99                         Log.e("Navit","Exception "+e.getMessage());
100                         return false;
101                 }
102         }
103         if (needs_update) {
104                 Log.e("Navit","Extracting resource");
105                 
106                 try {
107                         InputStream resourcestream=res.openRawResource(id);
108                         FileOutputStream resultfilestream=new FileOutputStream(resultfile);
109                         byte[] buf = new byte[1024];
110                         int i = 0;
111                         while ((i = resourcestream.read(buf)) != -1) {
112                                 resultfilestream.write(buf, 0, i);
113                         }
114                 } 
115                 catch (Exception e) {
116                         Log.e("Navit","Exception "+e.getMessage());
117                         return false;
118                 }
119         }
120         return true;
121     }
122     /** Called when the activity is first created. */
123     @Override
124     public void onCreate(Bundle savedInstanceState)
125     {
126         super.onCreate(savedInstanceState);
127         PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);  
128         wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "NavitDoNotDimScreen"); 
129         Locale locale=java.util.Locale.getDefault();
130         String lang=locale.getLanguage();
131         String langu=lang;
132         String langc=lang;
133         int pos=langu.indexOf('_');
134         if (pos != -1) {
135                 langc=langu.substring(0, pos);
136                 langu=langc + langu.substring(pos).toUpperCase(locale);
137         } else {
138                 String country=locale.getCountry();
139                 Log.e("Navit","Country "+country);
140                 langu=langc + "_" + country.toUpperCase(locale);
141         }
142         Log.e("Navit","Language " + lang);
143
144         if (!extractRes(langc,"/data/data/org.navitproject.navit/locale/"+langc+"/LC_MESSAGES/navit.mo")) {
145                 Log.e("Navit","Failed to extract language resource "+langc);
146         }
147         if (!extractRes("navit","/data/data/org.navitproject.navit/share/navit.xml")) {
148                 Log.e("Navit","Failed to extract navit.xml");
149         }
150         // Debug.startMethodTracing("calc");
151         NavitMain(this, langu);
152         NavitActivity(3);
153     }
154     @Override public void onStart()
155     {
156         super.onStart();
157         Log.e("Navit","OnStart");
158         NavitActivity(2);
159     }
160     @Override public void onRestart()
161     {
162         super.onRestart();
163         Log.e("Navit","OnRestart");
164         NavitActivity(0);
165     }
166     @Override public void onResume()
167     {
168         super.onResume();
169         Log.e("Navit","OnResume");
170         NavitActivity(1);
171     }
172     @Override public void onPause()
173     {
174         super.onPause();
175         Log.e("Navit","OnPause");
176         NavitActivity(-1);
177     }
178     @Override public void onStop()
179     {
180         super.onStop();
181         Log.e("Navit","OnStop");
182         NavitActivity(-2);
183     }
184     @Override public void onDestroy()
185     {
186         super.onDestroy();
187         Log.e("Navit","OnDestroy");
188         NavitActivity(-3);
189     }
190
191     public void disableSuspend()
192     {
193         wl.acquire();
194         wl.release();
195     }
196
197     public void exit()
198     {
199         finish();
200     }
201
202     public boolean handleMessage(Message m) {
203         Log.e("Navit","Handler received message");
204         return true;
205     }
206
207     /* A native method that is implemented by the
208      * 'hello-jni' native library, which is packaged
209      * with this application.
210      */
211     public native void NavitMain(Navit x, String lang);
212     public native void NavitActivity(int activity);
213
214     /* this is used to load the 'hello-jni' library on application
215      * startup. The library has already been unpacked into
216      * /data/data/com.example.Navit/lib/libhello-jni.so at
217      * installation time by the package manager.
218      */
219     static {
220         System.loadLibrary("navit");
221     }
222 }
223