App
FileOperationsToolSettingsFragment.java
Go to the documentation of this file.
1 package mhr.app.fragments.toolbox;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 
10 import mhr.app.AppMainActivity;
11 import mhr.app.R;
12 import mhr.appandroid.adapters.APDBitmap;
13 import mhr.appandroid.adapters.AndroidImageFile;
14 import mhr.appcore.AppCore;
15 import android.app.Activity;
16 import android.app.AlertDialog;
17 import android.app.Fragment;
18 import android.content.ActivityNotFoundException;
19 import android.content.DialogInterface;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.graphics.Bitmap.CompressFormat;
23 import android.media.MediaScannerConnection;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.os.Environment;
27 import android.util.Log;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.View.OnClickListener;
31 import android.view.ViewGroup;
32 import android.widget.AdapterView;
33 import android.widget.AdapterView.OnItemSelectedListener;
34 import android.widget.Button;
35 import android.widget.EditText;
36 import android.widget.SeekBar;
37 import android.widget.SeekBar.OnSeekBarChangeListener;
38 import android.widget.Spinner;
39 import android.widget.TextView;
40 import android.widget.Toast;
41 
45 public class FileOperationsToolSettingsFragment extends Fragment implements OnClickListener {
46 
47  //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
48  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
49  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
50 
51  //===== FIELDS ==============================================================================================================================//
52  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
53  protected View root = null;
55  protected AlertDialog exportDialog;
56  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
57 
58  //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
59  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
60 
61  protected void initExportDialog(LayoutInflater inflater) {
62  AlertDialog.Builder b = new AlertDialog.Builder(activity);
63  final View dv = inflater.inflate(R.layout.dialog_export_image, null, false);
64  ((Spinner)dv.findViewById(R.id.ExportFormatSp)).setOnItemSelectedListener(new OnItemSelectedListener() {
65 
66  View quality = dv.findViewById(R.id.ImageQualityValueGroup);
67 
68  @Override
69  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
70  if (position == 0) {
71  quality.setVisibility(quality.VISIBLE);
72  } else {
73  quality.setVisibility(quality.INVISIBLE);
74  }
75  }
76 
77  @Override
78  public void onNothingSelected(AdapterView<?> parent) {
79 
80  }
81  });
82 
83  ((SeekBar)dv.findViewById(R.id.ImageQualityValueSB)).setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
84 
85  TextView label = (TextView)dv.findViewById(R.id.ImageQualityValueTV);
86 
87  @Override
88  public void onStopTrackingTouch(SeekBar seekBar) {
89 
90  }
91 
92  @Override
93  public void onStartTrackingTouch(SeekBar seekBar) {
94 
95 
96  }
97 
98  @Override
99  public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
100  label.setText(Integer.toString(progress));
101  }
102  });
103 
104  b.setMessage("Please select output file name, format and quality.").setTitle("Export image to gallery").setView(dv);
105 
106  b.setPositiveButton("Export", new DialogInterface.OnClickListener() {
107 
108  final AppMainActivity a = activity;
109  final Spinner format = (Spinner)dv.findViewById(R.id.ExportFormatSp);
110  final EditText name = (EditText) dv.findViewById(R.id.ExportFileNameValueET);
111  final SeekBar quality = (SeekBar) dv.findViewById(R.id.ImageQualityValueSB);
112 
113  boolean ow = false;
114 
115  protected File getFile() {
116  File path = Environment.getExternalStoragePublicDirectory(
117  Environment.DIRECTORY_PICTURES);
118  return new File(path, name.getText().toString() + format.getSelectedItem().toString());
119  }
120 
121  protected void writeFile(File f) {
122 
123  AppCore c = a.getAppCore();
124  c.suspend(); // Nic nedela
125  Bitmap out = ((APDBitmap)c.getOutput()).getBitmap();
126  OutputStream os = null;
127  try {
128  f.getParentFile().mkdirs(); // Jestli cesta opravdu existuje
129 
130  os = new FileOutputStream(f);
131  switch (format.getSelectedItemPosition()) {
132  case 0:
133  out.compress(CompressFormat.JPEG, quality.getProgress(), os);
134  break;
135  case 1:
136  out.compress(CompressFormat.PNG, 100, os);
137  break;
138  default:
139  break;
140  }
141  os.flush();
142 
143  } catch (IOException e) {
144 
145  } finally {
146  if (os != null) {
147  try {
148  os.close();
149  } catch (IOException e) {
150 
151  }
152  }
153  }
154 
155  MediaScannerConnection.scanFile(activity, new String[] { f.toString() }, null, null);
156 
157  c.wake(); // Prozatim nic...
158 
159  }
160 
161  @Override
162  public void onClick(DialogInterface dialog, int which) {
163 
164  final File file = getFile();
165 
166  if (file.exists()) {
167  AlertDialog.Builder b = new AlertDialog.Builder(activity);
168  b.setMessage("Do you want to overwrite the file?").setTitle("File already exists");
169  b.setPositiveButton("Overwrite", new DialogInterface.OnClickListener() {
170 
171  File f = file;
172 
173  @Override
174  public void onClick(DialogInterface dialog, int which) {
175  writeFile(f);
176  }
177 
178  });
179 
180  b.setNegativeButton("Cancel", null);
181  b.create().show();
182 
183  return;
184  } else {
185  writeFile(file);
186  }
187 
188  }
189  });
190 
191  b.setNegativeButton("Cancel", null);
192 
193  exportDialog = b.create();
194 
195  }
196 
197  protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
198  View v = inflater.inflate(R.layout.fragment_file_operations_tool_settings, container, false);
199  root = v;
200  root.findViewById(R.id.CreateNewImageBtn).setOnClickListener(this);
201  root.findViewById(R.id.OpenGalleryImageBtn).setOnClickListener(this);
202  root.findViewById(R.id.OpenSavedImageBtn).setOnClickListener(this);
203  root.findViewById(R.id.SaveImageBtn).setOnClickListener(this);
204  root.findViewById(R.id.ExportImageBtn).setOnClickListener(this);
205 
206  initExportDialog(inflater);
207 
208  return v;
209  }
210  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
211 
212  //===== METHODS =============================================================================================================================//
213  //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
214 
215  //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
216 
217  //===== CALLBACKS ===========================================================================================================================//
218  public void onClick(View v) {
219  int id = v.getId();
220  switch (id) {
221  case R.id.CreateNewImageBtn:
223  break;
224  case R.id.OpenGalleryImageBtn:
225  Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
226  intent.setType("image/*");
227  intent.putExtra(Intent.EXTRA_TITLE, "Open image with:");
228  intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
229  try {
230  activity.startActivityForResult(intent, activity.OPEN_IMAGE_FROM_GALLERY_REQUEST_CODE);
231  } catch (ActivityNotFoundException e) {
232  e.printStackTrace();
233  }
234  break;
235  case R.id.OpenSavedImageBtn:
237  break;
238  case R.id.SaveImageBtn:
240  break;
241  case R.id.ExportImageBtn:
242  exportDialog.show();
243  break;
244  default:
245  break;
246  }
247  }
248 
249  //----- LIFE CYCLE --------------------------------------------------------------------------------------------------------------------------//
250 
251  @Override
252  public void onAttach(Activity activity) {
253  super.onAttach(activity);
254  /*
255  * called once the fragment is associated with its activity.
256  */
257  this.activity = (AppMainActivity) activity;
258  }
259 
260  // @Override
261  // public void onCreate(Bundle savedInstanceState) {
262  // super.onCreate(savedInstanceState);
263  // /*
264  // * called to do initial creation of the fragment.
265  // *
266  // * The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want
267  // * to retain when the fragment is paused or stopped, then resumed.
268  // */
269  // }
270 
271  @Override
272  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
273  // return super.onCreateView(inflater, container, savedInstanceState);
274  /*
275  * creates and returns the view hierarchy associated with the fragment.
276  *
277  * The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a
278  * View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
279  *
280  * Zde začíná životní cyklus při návratu z backstacku
281  */
282  if (root == null) {
283  return initView(inflater, container, savedInstanceState);
284  } else {
285  return root;
286  }
287  }
288 
289  // @Override
290  // public void onActivityCreated(Bundle savedInstanceState) {
291  // super.onActivityCreated(savedInstanceState);
292  // /*
293  // * tells the fragment that its activity has completed its own Activity.onCreate().
294  // */
295  // }
296 
297  // @Override // Vyžaduje API 17
298  // public void onViewStateRestored(Bundle savedInstanceState) {
299  // super.onViewStateRestored(savedInstanceState);
300  // /*
301  // * tells the fragment that all of the saved state of its view hierarchy has been restored.
302  // */
303  // }
304 
305  // @Override
306  // public void onStart() {
307  // super.onStart();
308  // /*
309  // * makes the fragment visible to the user (based on its containing activity being started).
310  // */
311  // }
312  //
313  // @Override
314  // public void onResume() {
315  // super.onResume();
316  // /*
317  // * makes the fragment interacting with the user (based on its containing activity being resumed).
318  // */
319  // }
320 
321  // @Override
322  // public void onPause() {
323  // super.onPause();
324  // /*
325  // * fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
326  // *
327  // * The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed).
328  // * This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
329  // */
330  // }
331 
332  // @Override
333  // public void onStop() {
334  // super.onStop();
335  // /*
336  // * fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
337  // */
338  // }
339 
340  // @Override
341  // public void onDestroyView() {
342  // super.onDestroyView();
343  // /*
344  // * allows the fragment to clean up resources associated with its View.
345  // *
346  // * Zde končí životní cyklus při umístění do backstacku
347  // */
348  // }
349 
350  // @Override
351  // public void onDestroy() {
352  // super.onDestroy();
353  // /*
354  // * called to do final cleanup of the fragment's state.
355  // */
356  // }
357 
358  // @Override
359  // public void onDetach() {
360  // super.onDetach();
361  // /*
362  // * called immediately prior to the fragment no longer being associated with its activity.
363  // */
364  // }
365 
366 }