348bbe3dd566b27d90ffd22b3a0e1a73e578483d
[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     }
153     @Override public void onStart()
154     {
155         super.onStart();
156         Log.e("Navit","OnStart");
157     }
158     @Override public void onRestart()
159     {
160         super.onRestart();
161         Log.e("Navit","OnRestart");
162     }
163     @Override public void onResume()
164     {
165         super.onResume();
166         Log.e("Navit","OnResume");
167     }
168     @Override public void onPause()
169     {
170         super.onPause();
171         Log.e("Navit","OnPause");
172     }
173     @Override public void onStop()
174     {
175         super.onStop();
176         Log.e("Navit","OnStop");
177     }
178     @Override public void onDestroy()
179     {
180         super.onDestroy();
181         Log.e("Navit","OnDestroy");
182     }
183
184     public void disableSuspend()
185     {
186         wl.acquire();
187         wl.release();
188     }
189
190     public void exit()
191     {
192         finish();
193     }
194
195     public boolean handleMessage(Message m) {
196         Log.e("Navit","Handler received message");
197         return true;
198     }
199
200     /* A native method that is implemented by the
201      * 'hello-jni' native library, which is packaged
202      * with this application.
203      */
204     public native void NavitMain(Navit x, String lang);
205
206     /* this is used to load the 'hello-jni' library on application
207      * startup. The library has already been unpacked into
208      * /data/data/com.example.Navit/lib/libhello-jni.so at
209      * installation time by the package manager.
210      */
211     static {
212         System.loadLibrary("navit");
213     }
214 }
215