/
SaveProductOfflineActivity.java
184 lines (158 loc) 路 6.82 KB
/
SaveProductOfflineActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package openfoodfacts.github.scrachx.openfood.views;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.SendProduct;
/**
* Created by scotscriven on 09/05/15.
*/
public class SaveProductOfflineActivity extends Activity{
ImageView imgSave;
EditText name, energy, store, weight;
Button takePic, save;
static final int REQUEST_TAKE_PHOTO = 1;
String mCurrentPhotoPath;
File photoFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save_product_offline);
final SharedPreferences settings = getSharedPreferences("img", 0);
settings.getString("imgUrl", "");
Intent intent = getIntent();
final String barcode = intent.getStringExtra("barcode");
imgSave = (ImageView) findViewById(R.id.imageSave);
name = (EditText) findViewById(R.id.editTextName);
energy = (EditText) findViewById(R.id.editTextEnergy);
store = (EditText) findViewById(R.id.editTextStores);
weight = (EditText) findViewById(R.id.editTextWeight);
takePic = (Button) findViewById(R.id.buttonTakePicture);
save = (Button) findViewById(R.id.buttonSaveProduct);
name.setSelected(false);
energy.setSelected(false);
store.setSelected(false);
weight.setSelected(false);
List<SendProduct> sp = SendProduct.find(SendProduct.class,"barcode = ?", barcode);
if(sp.size() > 0){
SendProduct product = sp.get(0);
Bitmap bt = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(product.getImgupload_front()), 400, 400, true);
imgSave.setImageBitmap(bt);
name.setText(product.getName());
energy.setText(product.getEnergy());
store.setText(product.getStores());
weight.setText(product.getWeight());
}
takePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dispatchTakePictureIntent(settings);
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!settings.getString("imgUrl", "").isEmpty() && !name.getText().toString().isEmpty()){
List<SendProduct> sp = SendProduct.find(SendProduct.class,"barcode = ?", barcode);
if(sp.size() > 0){
SendProduct product = sp.get(0);
product.setName(name.getText().toString());
product.setEnergy(energy.getText().toString());
product.setImgupload_front(settings.getString("imgUrl", ""));
product.setStores(store.getText().toString());
product.setWeight(weight.getText().toString());
product.save();
}else{
SendProduct product = new SendProduct(barcode, name.getText().toString(), energy.getText().toString(),
"kJ", weight.getText().toString(), settings.getString("imgUrl", ""), store.getText().toString());
product.save();
}
Toast.makeText(getApplicationContext(), R.string.txtDialogsContentInfoSave, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
SaveProductOfflineActivity.this.finish();
}else{
Toast.makeText(getApplicationContext(), R.string.txtPictureNeeded, Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
final SharedPreferences settings = getSharedPreferences("img", 0);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK){
Bitmap bt = BitmapFactory.decodeFile(settings.getString("imgUrl", ""));
imgSave.setImageBitmap(bt);
}
}
private void dispatchTakePictureIntent(SharedPreferences shpref) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
SharedPreferences.Editor editor = shpref.edit();
photoFile = createImageFile();
editor.putString("imgUrl", photoFile.getAbsolutePath());
editor.commit();
} catch (IOException ex) {
// Error occurred while creating the File
System.out.println(ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "PNG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".png", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}