浏览代码

No fast sync.
Images propperly downloaded.
Storage permissions.
No erros in location fetch.

Iñigo Valentin 8 年之前
父节点
当前提交
52597b25c5

+ 4 - 6
app/src/main/java/com/ivalentin/margolariak/ActivityLayout.java

@@ -139,9 +139,8 @@ public class ActivityLayout extends Fragment{
 						//If not, create directories and download asynchronously
 						File fpath;
 						fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
-						if (fpath.mkdirs()) {
-							new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
-						}
+						fpath.mkdirs();
+						new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
 					}
 				}
 				cursorImage.close();
@@ -237,9 +236,8 @@ public class ActivityLayout extends Fragment{
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
 				}
 			}
 			cursorImage.close();

+ 3 - 4
app/src/main/java/com/ivalentin/margolariak/ActivityPastLayout.java

@@ -94,9 +94,8 @@ public class ActivityPastLayout extends Fragment {
 				//If not, create directories and download asynchronously
 				File fpath;
 				fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/preview/");
-				if(fpath.mkdirs()) {
-					new DownloadImage(GM.API.SERVER + "/img/actividades/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/preview/" + image, images[i], GM.IMG.SIZE.PREVIEW).execute();
-				}
+				fpath.mkdirs();
+				new DownloadImage(GM.API.SERVER + "/img/actividades/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/preview/" + image, images[i], GM.IMG.SIZE.PREVIEW).execute();
 			 }
 			images[i].setVisibility(View.VISIBLE);
 			i ++;
@@ -107,4 +106,4 @@ public class ActivityPastLayout extends Fragment {
 		db.close();
 		return view;
 	}
-}
+}

+ 5 - 7
app/src/main/java/com/ivalentin/margolariak/AlbumLayout.java

@@ -123,9 +123,8 @@ public class AlbumLayout extends Fragment {
 				//If not, create directories and download asynchronously
 				File fpath;
 				fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
-				if (fpath.mkdirs()) {
-					new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivLeft, GM.IMG.SIZE.PREVIEW).execute();
-				}
+				fpath.mkdirs();
+				new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivLeft, GM.IMG.SIZE.PREVIEW).execute();
 			}
 
 			//Count comments
@@ -195,9 +194,8 @@ public class AlbumLayout extends Fragment {
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivRight, GM.IMG.SIZE.PREVIEW).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivRight, GM.IMG.SIZE.PREVIEW).execute();
 				}
 
 				//Count comments
@@ -235,4 +233,4 @@ public class AlbumLayout extends Fragment {
 		db.close();
 		return view;
 	}
-}
+}

+ 2 - 3
app/src/main/java/com/ivalentin/margolariak/BlogLayout.java

@@ -194,9 +194,8 @@ public class BlogLayout extends Fragment{
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
 				}
 			}
 			else{

+ 1 - 1
app/src/main/java/com/ivalentin/margolariak/DownloadImage.java

@@ -104,4 +104,4 @@ class DownloadImage extends AsyncTask<Void, Void, Void> {
 		}
 	}
 
-}
+}

+ 7 - 1
app/src/main/java/com/ivalentin/margolariak/GM.java

@@ -1115,7 +1115,13 @@ final class GM {
 	static final class PERMISSION {
 
 		/**
-		 * Code for the location permission request
+		 * Code for the storage persmission request.
+		 */
+
+		static final int STORAGE = 0;
+
+		/**
+		 * Code for the location permission request.
 		 */
 		static final int LOCATION = 1;
 	}

+ 2 - 3
app/src/main/java/com/ivalentin/margolariak/GalleryLayout.java

@@ -123,9 +123,8 @@ public class GalleryLayout extends Fragment{
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/galeria/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image, preview[i], GM.IMG.SIZE.MINIATURE).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/galeria/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/miniature/" + image, preview[i], GM.IMG.SIZE.MINIATURE).execute();
 				}
 
 				//Rotate the image

+ 30 - 18
app/src/main/java/com/ivalentin/margolariak/HomeLayout.java

@@ -59,6 +59,7 @@ public class HomeLayout extends Fragment implements LocationListener {
 
 	private View view;
 
+
 	/**
 	 * Run when the fragment is inflated.
 	 * Assigns views, gets the date and does the first call to the {//@link populate function}.
@@ -77,6 +78,11 @@ public class HomeLayout extends Fragment implements LocationListener {
 		//Load the layout.
 		view = inflater.inflate(R.layout.fragment_layout_home, container, false);
 
+		//Request storage permissions if not set
+		if (!checkStoragePermission()) {
+			requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, GM.PERMISSION.STORAGE);
+		}
+
 		//Request location permissions if not set
 		if (!checkLocationPermission()) {
 			requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, GM.PERMISSION.LOCATION);
@@ -448,9 +454,8 @@ public class HomeLayout extends Fragment implements LocationListener {
 						//If not, create directories and download asynchronously
 						File fpath;
 						fpath = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/");
-						if (fpath.mkdirs()) {
-							new DownloadImage(GM.API.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, ivImage, GM.IMG.SIZE.PREVIEW).execute();
-						}
+						fpath.mkdirs();
+						new DownloadImage(GM.API.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, ivImage, GM.IMG.SIZE.PREVIEW).execute();
 					}
 				}
 				else {
@@ -559,9 +564,8 @@ public class HomeLayout extends Fragment implements LocationListener {
 				//If not, create directories and download asynchronously
 				File fpath;
 				fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
-				if (fpath.mkdirs()){
-					new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivPhoto[counter], GM.IMG.SIZE.PREVIEW).execute();
-				}
+				fpath.mkdirs();
+				new DownloadImage(GM.API.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivPhoto[counter], GM.IMG.SIZE.PREVIEW).execute();
 			}
 
 			//Set listeners for images
@@ -691,9 +695,8 @@ public class HomeLayout extends Fragment implements LocationListener {
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
 				}
 			}
 			cursorImage.close();
@@ -820,9 +823,8 @@ public class HomeLayout extends Fragment implements LocationListener {
 						//If not, create directories and download asynchronously
 						File fpath;
 						fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
-						if (fpath.mkdirs()) {
-							new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
-						}
+						fpath.mkdirs();
+						new DownloadImage(GM.API.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
 					}
 				}
 
@@ -937,9 +939,8 @@ public class HomeLayout extends Fragment implements LocationListener {
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv, GM.IMG.SIZE.MINIATURE).execute();
 				}
 			}
 
@@ -1046,6 +1047,7 @@ public class HomeLayout extends Fragment implements LocationListener {
 		super.onPause();
 	}
 
+
 	/**
 	 * Called when the fragment is destroyed.
 	 * Stops the location manager
@@ -1063,11 +1065,12 @@ public class HomeLayout extends Fragment implements LocationListener {
 		}
 		super.onDestroy();
 	}
-	
+
+
 	/**
-	 * Called when the fragment is brought back into the foreground. 
+	 * Called when the fragment is brought back into the foreground.
 	 * Resumes the map and the location manager.
-	 * 
+	 *
 	 * @see android.app.Fragment#onResume()
 	 */
 	@Override
@@ -1078,6 +1081,15 @@ public class HomeLayout extends Fragment implements LocationListener {
 		super.onResume();
 	}
 
+
+	/**
+	 * Checks app permission to write device storage, user for OSM cache.
+	 * @return true if the permission has been granted, false otherwise.
+	 */
+        private boolean checkStoragePermission(){
+                return getContext().checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
+        }
+
 	/**
 	 * Checks app permission to access the user location.
 	 * @return true if the permission has been granted, false otherwise.

+ 3 - 4
app/src/main/java/com/ivalentin/margolariak/LablancaLayout.java

@@ -77,9 +77,8 @@ public class LablancaLayout extends Fragment {
 					//If not, create directories and download asynchronously
 					File fpath;
 					fpath = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/");
-					if (fpath.mkdirs()) {
-						new DownloadImage(GM.API.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, headerImage, GM.IMG.SIZE.PREVIEW).execute();
-					}
+					fpath.mkdirs();
+					new DownloadImage(GM.API.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, headerImage, GM.IMG.SIZE.PREVIEW).execute();
 				}
 			} else {
 				headerImage.setVisibility(View.GONE);
@@ -192,4 +191,4 @@ public class LablancaLayout extends Fragment {
 
 		return view;
 	}
-}
+}

+ 3 - 4
app/src/main/java/com/ivalentin/margolariak/PhotoLayout.java

@@ -220,9 +220,8 @@ public class PhotoLayout extends Fragment {
 			imageView.setImageResource(getResources().getIdentifier("com.ivalentin.margolariak:drawable/photo_placeholder", null, null));
 			File fpath;
 			fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/view/");
-			if (fpath.mkdirs()) {
-				new DownloadImage(GM.API.SERVER + "/img/galeria/view/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/view/" + image, imageView, GM.IMG.SIZE.VIEW).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
-			}
+			fpath.mkdirs();
+			new DownloadImage(GM.API.SERVER + "/img/galeria/view/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/view/" + image, imageView, GM.IMG.SIZE.VIEW).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
 		}
 
 		//Hide or show comments, as needed
@@ -297,4 +296,4 @@ public class PhotoLayout extends Fragment {
 		cursor.close();
 		db.close();
 	}
-}
+}

+ 3 - 4
app/src/main/java/com/ivalentin/margolariak/PostLayout.java

@@ -101,9 +101,8 @@ public class PostLayout extends Fragment {
 				//If not, create directories and download asynchronously
 				File fpath;
 				fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/preview/");
-				if (fpath.mkdirs()) {
-					new DownloadImage(GM.API.SERVER + "/img/blog/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/preview/" + image, images[i], GM.IMG.SIZE.PREVIEW).execute();
-				}
+				fpath.mkdirs();
+				new DownloadImage(GM.API.SERVER + "/img/blog/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/preview/" + image, images[i], GM.IMG.SIZE.PREVIEW).execute();
 			}
 			images[i].setVisibility(View.VISIBLE);
 			i ++;
@@ -167,4 +166,4 @@ public class PostLayout extends Fragment {
 		db.close();
 		return view;
 	}
-}
+}

+ 7 - 0
app/src/main/java/com/ivalentin/margolariak/ReceiveLocation.java

@@ -70,6 +70,10 @@ class ReceiveLocation extends AsyncTask<Void, Void, Void> {
 
 					//Insert into the database
 					SQLiteDatabase db = activity.openOrCreateDatabase(GM.DB.NAME, Activity.MODE_PRIVATE, null);
+					if (db.isReadOnly()){
+						Log.e("RECEIVE_LOCATION", "Database is in read only mode. Skipping.");
+						return null;
+					}
 					db.execSQL("INSERT INTO location VALUES ('" + dtime + "', " + lat + ", " + lon + ")");
 					db.close();
 
@@ -85,6 +89,9 @@ class ReceiveLocation extends AsyncTask<Void, Void, Void> {
 		catch (IOException e) {
 			Log.e("RECEIVE_LOCATION", "Unable to get location, IO exception: " + e.toString());
 		}
+		catch (Exception e) {
+			Log.e("RECEIVE_LOCATION", "Unable to get location, unknown exception: " + e.toString());
+		}
 
 
 		return null;

+ 4 - 11
app/src/main/java/com/ivalentin/margolariak/Sync.java

@@ -267,17 +267,10 @@ class Sync extends AsyncTask<Void, Void, Void> {
 		try {
 
 			// Common parameters
-			url = GM.API.SERVER;
-			if (pbSync != null) { // First sync
-				url = url + GM.API.SYNC.PATH.FAST;
-			}
-			else{ // Regular sync
-				url = url + GM.API.SYNC.PATH.COMPLETE;
-			}
-			url = url + "?" +
-					GM.API.SYNC.KEY.CLIENT + "=" + URLEncoder.encode(GM.API.CLIENT, "UTF-8") + "&" +
-					GM.API.SYNC.KEY.USER + "=" + URLEncoder.encode(user, "UTF-8") + "&" +
-					GM.API.SYNC.KEY.FOREGROUND + "=" + foreground + "&";
+			url = GM.API.SERVER + GM.API.SYNC.PATH.COMPLETE + "?" +
+			  GM.API.SYNC.KEY.CLIENT + "=" + URLEncoder.encode(GM.API.CLIENT, "UTF-8") + "&" +
+			  GM.API.SYNC.KEY.USER + "=" + URLEncoder.encode(user, "UTF-8") + "&" +
+			  GM.API.SYNC.KEY.FOREGROUND + "=" + foreground;
 
 			// Versions of the tables
 			Cursor cursor;

+ 1 - 1
app/src/main/res/layout/fragment_layout_location.xml

@@ -50,7 +50,7 @@
 				android:layout_height="wrap_content"
 				android:layout_margin="10dp"
 				android:text="@string/home_section_location_text_calculating"
-				android:textAppearance="?android:attr/textAppearanceSmall"/>
+				android:textAppearance="?android:attr/textAppearanceMedium"/>
 
 			<LinearLayout
 				android:layout_width="match_parent"