Permalink
Browse files

重新修改数据保存接口,默认为只读模式

  • Loading branch information...
1 parent 962b472 commit 2e8ce78977e653f229599d69ed9b9941bb538095 @feelinglucky committed Apr 14, 2012
View
@@ -55,6 +55,12 @@
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
+ <activity android:name=".activity.Detail"
+ android:theme="@style/Window">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ </intent-filter>
+ </activity>
<service
android:name=".service.Recoder"
android:label="@string/service_name"
View
@@ -41,4 +41,5 @@
<string name="use_resume_archive_file">Use Resume File: %s</string>
<string name="storage_not_presented">Storage Not Presented</string>
<string name="calendar">Select Month</string>
+ <string name="archive_not_exists">archive not exists</string>
</resources>
@@ -16,11 +16,10 @@
import com.markupartist.android.widget.ActionBar;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
public class BaiduMap extends MapActivity {
- private static final String ARCHIVE_FILE_NAME = "archiveName";
- private String archiveFileName;
private Archive archive;
private MapView mapView;
private MapController mapViewController;
@@ -29,6 +28,7 @@
private BMapManager bMapManager = null;
private ActionBar actionBar;
private UIHelper uiHelper;
+ private String archiveFileName;
@Override
protected boolean isRouteDisplayed() {
@@ -71,8 +71,13 @@ public void onCreate(Bundle savedInstanceState) {
mapViewController = mapView.getController();
uiHelper = new UIHelper(context);
- archiveFileName = getIntent().getStringExtra(ARCHIVE_FILE_NAME);
- archive = new Archive(getApplicationContext(), archiveFileName);
+ archiveFileName = getIntent().getStringExtra(Records.INTENT_ARCHIVE_FILE_NAME);
+ try {
+ archive = new Archive(getApplicationContext(), archiveFileName);
+ } catch (IOException e) {
+ uiHelper.showLongToast(getString(R.string.archive_not_exists));
+ finish();
+ }
locations = archive.fetchAll();
@@ -19,10 +19,12 @@
protected UIHelper uiHelper;
public Intent recordServerIntent;
protected ActionBar actionBar;
+ protected Base context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
+ context = this;
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
uiHelper = new UIHelper(this);
@@ -0,0 +1,36 @@
+package com.gracecode.tracker.activity;
+
+import android.os.Bundle;
+import com.gracecode.tracker.R;
+import com.gracecode.tracker.dao.Archive;
+import com.gracecode.tracker.dao.ArchiveMeta;
+
+import java.io.IOException;
+
+public class Detail extends Base {
+ private String archiveFileName;
+ private Archive archive;
+ private ArchiveMeta archiveMeta;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ archiveFileName = getIntent().getStringExtra(Records.INTENT_ARCHIVE_FILE_NAME);
+ try {
+ archive = new Archive(context, archiveFileName);
+ archiveMeta = archive.getArchiveMeta();
+ } catch (IOException e) {
+ uiHelper.showLongToast(getString(R.string.archive_not_exists));
+ finish();
+ }
+ }
+
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ archive.close();
+ }
+
+}
@@ -7,10 +7,12 @@
import android.graphics.Point;
import android.location.Location;
import android.os.Bundle;
-import com.google.android.maps.*;
+import com.baidu.mapapi.*;
import com.gracecode.tracker.R;
import com.gracecode.tracker.dao.Archive;
+import com.gracecode.tracker.util.UIHelper;
+import java.io.IOException;
import java.util.ArrayList;
public class GoogleMap extends MapActivity {
@@ -21,6 +23,7 @@
private Context context;
private Archive archive;
private ArrayList<Location> locations;
+ private UIHelper uiHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
@@ -29,10 +32,15 @@ public void onCreate(Bundle savedInstanceState) {
mapView = (MapView) findViewById(R.id.google_mapview);
mapController = mapView.getController();
-
+ uiHelper = new UIHelper(context);
context = getApplicationContext();
- archive = new Archive(context, getIntent().getStringExtra(ARCHIVE_FILE_NAME));
+ try {
+ archive = new Archive(context, getIntent().getStringExtra(ARCHIVE_FILE_NAME));
+ } catch (IOException e) {
+ uiHelper.showLongToast(getString(R.string.archive_not_exists));
+ finish();
+ }
locations = archive.fetchAll();
mapView.getOverlays().add(new WalkedOverlay());
@@ -14,14 +14,16 @@
import com.gracecode.tracker.dao.Archive;
import com.gracecode.tracker.dao.ArchiveMeta;
import com.gracecode.tracker.service.ArchiveNameHelper;
+import com.gracecode.tracker.util.Logger;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class Records extends Base implements AdapterView.OnItemClickListener {
private Context context;
- private static final String ARCHIVE_FILE_NAME = "archiveName";
+ public static final String INTENT_ARCHIVE_FILE_NAME = "archiveName";
private ListView listView;
private ArrayList<String> archiveFileNames;
@@ -34,7 +36,7 @@
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Archive archive = archives.get(i);
Intent intent = new Intent(this, BaiduMap.class);
- intent.putExtra(ARCHIVE_FILE_NAME, archive.getArchiveFileName());
+ intent.putExtra(INTENT_ARCHIVE_FILE_NAME, archive.getArchiveFileName());
startActivity(intent);
}
@@ -116,7 +118,12 @@ private void openArchivesFromFileNames() {
Iterator<String> iterator = archiveFileNames.iterator();
while (iterator.hasNext()) {
String name = (String) iterator.next();
- archives.add(new Archive(context, name));
+ try {
+ archives.add(new Archive(context, name));
+ } catch (IOException e) {
+ Logger.e(getString(R.string.archive_not_exists));
+ continue;
+ }
}
}
@@ -10,6 +10,8 @@
import android.location.Location;
import com.gracecode.tracker.util.Logger;
+import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
public class Archive {
@@ -87,24 +89,49 @@ public Archive(Context context) {
geoPoints = new ArrayList<Location>();
}
- public Archive(Context context, String name) {
+ public Archive(Context context, String name) throws IOException {
this.context = context;
geoPoints = new ArrayList<Location>();
+ this.open(name);
+ }
+ public Archive(Context context, String name, int mode) throws IOException {
+ this.context = context;
+ geoPoints = new ArrayList<Location>();
this.open(name);
}
public String getArchiveFileName() {
return archiveName;
}
- public void open(String name) {
+ public void open(String name) throws IOException {
+ File f = new File(name);
+ if (!f.exists()) {
+ throw new IOException();
+ }
+
this.archiveName = name;
databaseHelper = new ArchiveDatabaseHelper(context, name);
- database = databaseHelper.getWritableDatabase();
+ database = databaseHelper.getReadableDatabase();
archiveMeta = new ArchiveMeta(this);
}
+ public void openOrCreate(String name) {
+ File file = new File(name);
+ try {
+ if (!file.exists()) {
+ file.createNewFile();
+ }
+ open(name);
+
+ // 重新开启可读写的数据库
+ database = databaseHelper.getWritableDatabase();
+ } catch (IOException e) {
+
+ }
+ }
+
public ArchiveMeta getArchiveMeta() {
return archiveMeta;
}
@@ -42,10 +42,10 @@
public class Recoder extends Service {
protected Recoder.ServiceBinder serviceBinder;
private SharedPreferences sharedPreferences;
- private Archive geoArchive;
+ private Archive archive;
private Listener listener;
- private LocationManager locationManager;
+ private static LocationManager locationManager = null;
private ArchiveNameHelper archiveFileNameHelper;
private String archiveFileName;
@@ -57,9 +57,9 @@
private ArchiveMeta meta = null;
ServiceBinder() {
+ archive = new Archive(getApplicationContext());
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
- geoArchive = new Archive(getApplicationContext());
- listener = new Listener(geoArchive);
+ listener = new Listener(archive);
}
@Override
@@ -82,7 +82,7 @@ public void startRecord() {
}
try {
- geoArchive.open(archiveFileName);
+ archive.openOrCreate(archiveFileName);
archiveFileNameHelper.setLastOpenedArchiveFileName(archiveFileName);
// 获取 Meta 信息
@@ -94,6 +94,7 @@ public void startRecord() {
// 绑定 GPS 回调
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
minTime, minDistance, listener);
+
} catch (SQLiteException e) {
Logger.e(e.getMessage());
}
@@ -121,7 +122,7 @@ public void stopRecord() {
}
// 清除操作
- geoArchive.close();
+ archive.close();
meta = null;
archiveFileNameHelper.clearLastOpenedArchiveFileName();
@@ -136,17 +137,17 @@ public int getStatus() {
@Override
public ArchiveMeta getArchiveMeta() {
- return geoArchive.getArchiveMeta();
+ return archive.getArchiveMeta();
}
@Override
public Archive getArchive() {
- return geoArchive;
+ return archive;
}
@Override
public Location getLastRecord() {
- return geoArchive.getLastRecord();
+ return archive.getLastRecord();
}
}

0 comments on commit 2e8ce78

Please sign in to comment.