mirror of
https://github.com/derekbrameyer/MacroDroid.git
synced 2026-09-03 07:15:14 +08:00
Import and Export functions for My Foods finished.
This commit is contained in:
@@ -5,7 +5,8 @@
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="7"
|
||||
android:targetSdkVersion="14" />
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<TableRow >
|
||||
<TableRow>
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
@@ -32,7 +32,7 @@
|
||||
android:inputType="textCapWords" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
<TableRow>
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
@@ -44,7 +44,7 @@
|
||||
android:inputType="text" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
<TableRow>
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
@@ -55,7 +55,7 @@
|
||||
style="@style/MyMacrosIKnowRightColumnET" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
<TableRow>
|
||||
|
||||
<TextView
|
||||
style="@style/MyMacrosLeftColumnTL"
|
||||
@@ -77,7 +77,7 @@
|
||||
android:layout_marginTop="10dp"
|
||||
android:text="@string/Macros" />
|
||||
|
||||
<TableRow >
|
||||
<TableRow>
|
||||
|
||||
<TextView
|
||||
style="@style/ThreeColumnCenteredTV"
|
||||
@@ -92,7 +92,7 @@
|
||||
android:text="@string/fat" />
|
||||
</TableRow>
|
||||
|
||||
<TableRow >
|
||||
<TableRow>
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ET_add_food_protein"
|
||||
@@ -122,15 +122,15 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_add"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Add" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_cancel"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Cancel" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_add"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Add" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
@@ -117,15 +117,15 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal" >
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_save"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Save" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_cancel"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Cancel" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/BTN_save"
|
||||
style="@style/TwoColumnButtons"
|
||||
android:text="@string/Save" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
</ScrollView>
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
<item
|
||||
android:id="@+id/MENU_save_foods"
|
||||
android:icon="@android:drawable/ic_menu_save"
|
||||
android:showAsAction="ifRoom"
|
||||
android:title="@string/Save_Foods"/>
|
||||
<item
|
||||
android:id="@+id/MENU_import"
|
||||
android:icon="@drawable/ic_menu_archive"
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
<string name="hello">Hello World, MainActivity!</string>
|
||||
<string name="activity_multiplier">activity multiplier [1.0-2.0]</string>
|
||||
<string name="age">age</string>
|
||||
<string name="import_instructions">To import new foods from a file, edit /sdcard/MacroDroid/myfoods.txt keeping in mind that formatting matters!</string>
|
||||
<string name="export_instructions">Your foods will be exported to /sdcard/MacroDroid/myfoods.txt</string>
|
||||
<string name="app_name">MacroDroid</string>
|
||||
<string name="body_fat">body fat</string>
|
||||
<string name="gender">gender</string>
|
||||
@@ -57,6 +59,7 @@
|
||||
<string name="Rest_Day">Rest Day</string>
|
||||
<string name="RMR">RMR</string>
|
||||
<string name="Save">Save</string>
|
||||
<string name="Save_Foods">Save Foods</string>
|
||||
<string name="Save_Food_Group">Save Food Group</string>
|
||||
<string name="total_servings">total servings</string>
|
||||
<string name="Serving_Size">Serving Size</string>
|
||||
|
||||
@@ -61,7 +61,7 @@ public class DayDetailActivity extends FragmentActivity {
|
||||
ab.setDisplayUseLogoEnabled(false);
|
||||
ab.setDisplayHomeAsUpEnabled(false);
|
||||
ab.setDisplayShowHomeEnabled(false);
|
||||
|
||||
|
||||
noFoodsYetTV = (TextView) findViewById(R.id.TV_no_foods_yet);
|
||||
caloriesTV = (TextView) findViewById(R.id.TV_day_detail_calories);
|
||||
currentProteinTV = (TextView) findViewById(R.id.TV_day_detail_current_protein);
|
||||
@@ -228,22 +228,25 @@ public class DayDetailActivity extends FragmentActivity {
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == CAMERA_PIC_REQUEST) {
|
||||
Log.d(Tags.LOG_TAG, "CAMERA_PIC_REQUEST result.");
|
||||
Bitmap bmp = (Bitmap) data.getExtras().get("data");
|
||||
String saveBmpResults = application.saveBitmapToFile(
|
||||
bmp,
|
||||
Tags.PHOTOS_PATH,
|
||||
mFilePath
|
||||
+ "_"
|
||||
+ application.numPicsForDay(Tags.PHOTOS_PATH,
|
||||
mFilePath) + ".png");
|
||||
if (saveBmpResults.length() > 0) {
|
||||
Toast.makeText(mContext, "Saved picture to " + saveBmpResults,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(
|
||||
mContext,
|
||||
"Could not save the picture. Please make sure your SD card is mounted.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
if (data.getExtras() != null) {
|
||||
Bitmap bmp = (Bitmap) data.getExtras().get("data");
|
||||
String saveBmpResults = application.saveBitmapToFile(
|
||||
bmp,
|
||||
Tags.PHOTOS_PATH,
|
||||
mFilePath
|
||||
+ "_"
|
||||
+ application.numPicsForDay(Tags.PHOTOS_PATH,
|
||||
mFilePath) + ".png");
|
||||
if (saveBmpResults.length() > 0) {
|
||||
Toast.makeText(mContext,
|
||||
"Saved picture to " + saveBmpResults,
|
||||
Toast.LENGTH_SHORT).show();
|
||||
} else {
|
||||
Toast.makeText(
|
||||
mContext,
|
||||
"Could not save the picture. Please make sure your SD card is mounted.",
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,8 +272,9 @@ public class DayDetailActivity extends FragmentActivity {
|
||||
if (item == 0) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(Uri.parse("file://"
|
||||
+ File.separator + Tags.PHOTOS_PATH), "image/*");
|
||||
intent.setDataAndType(
|
||||
Uri.parse("file://" + File.separator
|
||||
+ Tags.PHOTOS_PATH), "image/*");
|
||||
startActivity(intent);
|
||||
|
||||
} else {
|
||||
@@ -297,7 +301,7 @@ public class DayDetailActivity extends FragmentActivity {
|
||||
return false;
|
||||
case R.id.MENU_weight:
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
|
||||
alert.setTitle("Weight");
|
||||
alert.setTitle("Set Weight");
|
||||
// Set an EditText view to get user input
|
||||
final EditText input = new EditText(mContext);
|
||||
input.append("100.0");
|
||||
|
||||
@@ -49,8 +49,7 @@ public class DayDetailAddFoodTabActivity extends TabActivity {
|
||||
// Initialize a TabSpec for each tab and add it to the TabHost
|
||||
spec = tabHost
|
||||
.newTabSpec("my_foods")
|
||||
.setIndicator("My Foods",
|
||||
res.getDrawable(R.drawable.ic_tab_my_foods))
|
||||
.setIndicator("My Foods")
|
||||
.setContent(intent);
|
||||
tabHost.addTab(spec);
|
||||
|
||||
@@ -61,8 +60,7 @@ public class DayDetailAddFoodTabActivity extends TabActivity {
|
||||
|
||||
spec = tabHost
|
||||
.newTabSpec("my_food_groups")
|
||||
.setIndicator("My Food Groups",
|
||||
res.getDrawable(R.drawable.ic_tab_my_food_groups))
|
||||
.setIndicator("Food Groups")
|
||||
.setContent(intent);
|
||||
tabHost.addTab(spec);
|
||||
|
||||
@@ -71,8 +69,7 @@ public class DayDetailAddFoodTabActivity extends TabActivity {
|
||||
|
||||
spec = tabHost
|
||||
.newTabSpec("new_food")
|
||||
.setIndicator("New Food",
|
||||
res.getDrawable(R.drawable.ic_tab_new_food))
|
||||
.setIndicator("New Food")
|
||||
.setContent(intent);
|
||||
tabHost.addTab(spec);
|
||||
|
||||
|
||||
@@ -19,7 +19,6 @@ import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MacroDroidApplication extends Application {
|
||||
private static final String MACROS_PREFS = "MacroDroidMacrosPrefs";
|
||||
@@ -236,6 +235,38 @@ public class MacroDroidApplication extends Application {
|
||||
return allFoodsArrayList;
|
||||
}
|
||||
|
||||
public byte[] readFromSdCard() {
|
||||
int len = 1024;
|
||||
byte[] buffer = new byte[len];
|
||||
try {
|
||||
File file = new File(Environment.getExternalStorageDirectory()
|
||||
+ File.separator + "MacroDroid");
|
||||
file.mkdirs();
|
||||
File file2 = new File(Environment.getExternalStorageDirectory()
|
||||
+ File.separator + "MacroDroid" + File.separator
|
||||
+ "myfoods.txt");
|
||||
FileInputStream fis = new FileInputStream(file2);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
int nrb = fis.read(buffer, 0, len);
|
||||
while (nrb != -1) {
|
||||
baos.write(buffer, 0, nrb);
|
||||
nrb = fis.read(buffer, 0, len);
|
||||
}
|
||||
buffer = baos.toByteArray();
|
||||
fis.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
// TODO If not found, write the default_foods.txt res/raw file to it
|
||||
e.printStackTrace();
|
||||
writeInternalStoragePrivate(Tags.MY_FOODS_FILENAME,
|
||||
defaultFoodsToByteArray());
|
||||
return readInternalStoragePrivate(Tags.MY_FOODS_FILENAME);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public byte[] readInternalStoragePrivate(String filename) {
|
||||
int len = 1024;
|
||||
byte[] buffer = new byte[len];
|
||||
@@ -254,13 +285,51 @@ public class MacroDroidApplication extends Application {
|
||||
e.printStackTrace();
|
||||
writeInternalStoragePrivate(Tags.MY_FOODS_FILENAME,
|
||||
defaultFoodsToByteArray());
|
||||
readInternalStoragePrivate(Tags.MY_FOODS_FILENAME);
|
||||
return readInternalStoragePrivate(Tags.MY_FOODS_FILENAME);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void exportFoodsToSdCard(byte[] foods) {
|
||||
if (!Environment.getExternalStorageState().equals(
|
||||
Environment.MEDIA_MOUNTED)) {
|
||||
} else {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
File dir = new File(Environment.getExternalStorageDirectory()
|
||||
+ File.separator + "MacroDroid");
|
||||
dir.mkdirs();
|
||||
File f = new File(Environment.getExternalStorageDirectory()
|
||||
+ File.separator + "MacroDroid" + File.separator
|
||||
+ "myfoods.txt");
|
||||
Log.d(Tags.LOG_TAG, "Filedir: " + f.getAbsolutePath());
|
||||
try {
|
||||
// write the bytes in file
|
||||
FileOutputStream fo = new FileOutputStream(f);
|
||||
fo.write(foods);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeToSdCard(ArrayList<AFood> foods) {
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
for (int i = 0; i < foods.size(); i++) {
|
||||
AFood f = foods.get(i);
|
||||
baos.write(foodToByteArray(f));
|
||||
}
|
||||
byte[] newFoodList = baos.toByteArray();
|
||||
exportFoodsToSdCard(newFoodList);
|
||||
} catch (IOException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void writeInternalStoragePrivate(String filename, byte[] content) {
|
||||
try {
|
||||
FileOutputStream fos = openFileOutput(filename,
|
||||
|
||||
@@ -104,9 +104,59 @@ public class MyFoodsActivity extends FragmentActivity {
|
||||
return false;
|
||||
case R.id.MENU_import:
|
||||
// TODO Import from file
|
||||
application.writeInternalStoragePrivate(Tags.MY_FOODS_FILENAME,
|
||||
application.defaultFoodsToByteArray());
|
||||
refreshList();
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
|
||||
alert.setTitle("Import from file");
|
||||
alert.setMessage(getResources().getString(
|
||||
R.string.import_instructions));
|
||||
alert.setCancelable(true);
|
||||
alert.setIcon(getResources()
|
||||
.getDrawable(R.drawable.ic_menu_archive));
|
||||
alert.setPositiveButton("Import",
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
application.writeInternalStoragePrivate(
|
||||
Tags.MY_FOODS_FILENAME,
|
||||
application.readFromSdCard());
|
||||
refreshList();
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
alert.setNegativeButton("Cancel",
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
alert.show();
|
||||
return false;
|
||||
case R.id.MENU_save_foods:
|
||||
// TODO Export to file
|
||||
AlertDialog.Builder alert2 = new AlertDialog.Builder(mContext);
|
||||
alert2.setTitle("Export to file");
|
||||
alert2.setMessage(getResources().getString(
|
||||
R.string.export_instructions));
|
||||
alert2.setCancelable(true);
|
||||
alert2.setIcon(getResources()
|
||||
.getDrawable(android.R.drawable.ic_menu_save));
|
||||
alert2.setPositiveButton("Export",
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
application.writeToSdCard(myFoodsArrayList);
|
||||
Toast.makeText(mContext, "Exported!", Toast.LENGTH_SHORT).show();
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
alert2.setNegativeButton("Cancel",
|
||||
new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
alert2.show();
|
||||
return false;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
|
||||
Reference in New Issue
Block a user