Procházet zdrojové kódy

Server settings parsed.

Settings received from the server (about comment posting, photo uploads and festivals) are no longer stored in a database, but in a data file.

Also, the sync dialogs have been styled a bit, and a debug case, forcing sync to fail, has been corrected.
seavenois před 9 roky
rodič
revize
d6295960cf

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

@@ -23,8 +23,16 @@ final class GM {
 	 */
 	final class DB {
 
+		/**
+		 * Name of the database.
+		 */
 		static final String NAME = "gm";
 
+		/**
+		 * Initial version of the database.
+		 */
+		static final int INITIAL_VERSION = 0;
+
 		/**
 		 * Column types
 		 */
@@ -605,9 +613,19 @@ final class GM {
 			static final String PREVIOUS_APP_VERSION = "previous_app_version";
 
 			/**
-			 * Key of the data to store if ther is a festival season..
+			 * Key of the data to store if ther is a festival season.
 			 */
 			static final String LABLANCA = "lablanca";
+
+			/**
+			 * Key that indicates if comments can be posted.
+			 */
+			static final String COMMENTS = "comments";
+
+			/**
+			 * Key that indicates if photos can be uploaded.
+			 */
+			static final String PHOTOS = "photos";
 		}
 
 		/**
@@ -630,6 +648,16 @@ final class GM {
 			 * Default value for the data that indicates if it's festival season.
 			 */
 			static final boolean LABLANCA = false;
+
+			/**
+			 * Default value for the key that indicates if comments can be posted.
+			 */
+			static final boolean COMMENTS = false;
+
+			/**
+			 * Default value for the key that indicates if photos can be uploaded.
+			 */
+			static final boolean PHOTOS = false;
 		}
 	}
 

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

@@ -360,7 +360,7 @@ public class MainActivity extends Activity {
 		});
 
 		//Get preferences
-		SharedPreferences sharedData = getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
+		SharedPreferences sharedData = getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
 		SharedPreferences.Editor dataEditor = sharedData.edit();
 
 		//If the user code is not set, generate one

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

@@ -19,11 +19,15 @@ import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.database.DatabaseUtils;
 import android.database.sqlite.SQLiteDatabase;
+import android.graphics.Color;
+import android.graphics.drawable.ColorDrawable;
 import android.os.AsyncTask;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.Window;
+import android.view.WindowManager;
 import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
@@ -88,7 +92,7 @@ class Sync extends AsyncTask<Void, Void, Void> {
 			doProgress = true;
 
 		}
-		Log.d("Sync", "Starting full sync");
+		Log.d("SYNC", "Starting full sync");
 	}
 	
 	/**
@@ -114,7 +118,7 @@ class Sync extends AsyncTask<Void, Void, Void> {
 			//Check db version again
 			SQLiteDatabase db = myContextRef.openOrCreateDatabase(GM.DB.NAME, Activity.MODE_PRIVATE, null);
 			if (db.isReadOnly()){
-				Log.e("Db ro", "Database is locked and in read only mode. Skipping sync.");
+				Log.e("SYNC", "Database is locked and in read only mode. Skipping sync.");
 				return;
 			}
 
@@ -128,7 +132,7 @@ class Sync extends AsyncTask<Void, Void, Void> {
 			db.close();
 
 			//If the database is on it's initial version (i.e: There is no data, new or old)
-			if (true){ //TODO dbVersion == GM.DATA.DEFAULT.DEFAULT_PREF_DB_VERSION){
+			if (dbVersion == GM.DB.INITIAL_VERSION){
 
 				Log.e("SYNC", "Full sync failed");
 
@@ -149,7 +153,17 @@ class Sync extends AsyncTask<Void, Void, Void> {
 					activity.finish();
 					}
 				});
-				
+
+				//Set dialog parameters
+				WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
+				//noinspection ConstantConditions
+				lp.copyFrom(dial.getWindow().getAttributes());
+				lp.width = WindowManager.LayoutParams.MATCH_PARENT;
+				lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
+				lp.gravity = Gravity.CENTER;
+				dial.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
+				dial.getWindow().setAttributes(lp);
+
 				//Show the dialog
 				dial.show();
 			}
@@ -269,6 +283,7 @@ class Sync extends AsyncTask<Void, Void, Void> {
 	 *
 	 * @return False if there were errors, true otherwise.
 	 */
+	@SuppressWarnings("unused")
 	private boolean recreateDb(SQLiteDatabase db) {
 		boolean result = true;
 		try {
@@ -455,6 +470,65 @@ class Sync extends AsyncTask<Void, Void, Void> {
 		return result;
 	}
 
+	/**
+	 * When the table "settings cames up, dont make a table, but store required
+	 * values as data.
+	 *
+	 * @param settings JSON string with the contents of the "settings" table.
+	 * @return true if values could be saved, false otherwise.
+	 */
+	private boolean saveSettings(String settings){
+
+		SharedPreferences sharedData = myContextRef.getSharedPreferences(GM.DATA.DATA, Context.MODE_PRIVATE);
+		SharedPreferences.Editor editor = sharedData.edit();
+		try{
+			String value;
+			if (settings.contains("\"name\":\"comments\",\"value\":\"")){
+				value = settings.substring(settings.indexOf("\"name\":\"comments\",\"value\":\"") + 27, settings.indexOf("\"name\":\"comments\",\"value\":\"") + 28);
+				if ("0".equals(value) || "1".equals(value)){
+					Log.d("SYNC", "Setting found: comments = " + value);
+					if ("0".equals(value)) {
+						editor.putBoolean(GM.DATA.KEY.COMMENTS, false);
+					}
+					else {
+						editor.putBoolean(GM.DATA.KEY.COMMENTS, true);
+					}
+				}
+			}
+			if (settings.contains("\"name\":\"festivals\",\"value\":\"")){
+				value = settings.substring(settings.indexOf("\"name\":\"festivals\",\"value\":\"") + 28, settings.indexOf("\"name\":\"festivals\",\"value\":\"") + 29);
+				if ("0".equals(value) || "1".equals(value)){
+					Log.d("SYNC", "Setting found: lablanca = " + value);
+					if ("0".equals(value)) {
+						editor.putBoolean(GM.DATA.KEY.LABLANCA, false);
+					}
+					else {
+						editor.putBoolean(GM.DATA.KEY.LABLANCA, true);
+					}
+				}
+			}
+			if (settings.contains("\"name\":\"photos\",\"value\":\"")){
+				value = settings.substring(settings.indexOf("\"name\":\"photos\",\"value\":\"") + 25, settings.indexOf("\"name\":\"photos\",\"value\":\"") + 26);
+				if ("0".equals(value) || "1".equals(value)){
+					Log.d("SYNC" ,"Setting found: photos = " + value);
+					if ("0".equals(value)) {
+						editor.putBoolean(GM.DATA.KEY.PHOTOS, false);
+					}
+					else {
+						editor.putBoolean(GM.DATA.KEY.PHOTOS, true);
+					}
+				}
+			}
+
+			editor.apply();
+			return true;
+		}
+		catch (Exception ex){
+			editor.apply();
+			Log.e("SYNC", "Unable to save settings: " + ex.toString());
+			return false;
+		}
+	}
 
 
 
@@ -468,6 +542,12 @@ class Sync extends AsyncTask<Void, Void, Void> {
 	 * @return False if there were errors, true otherwise.
 	 */
 	private boolean saveTable(SQLiteDatabase db, String table, String data) {
+
+		//If settings table, do something else
+		if ("settings".equals(table)){
+			return saveSettings(data);
+		}
+
 		int type, i;
 		JSONObject jsonObj;
 		String[] values = new String[99];

+ 11 - 10
app/src/main/res/layout/dialog_sync.xml

@@ -1,9 +1,10 @@
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:layout_width="fill_parent"
-              android:layout_height="wrap_content"
-              android:background="@drawable/section"
-              android:orientation="vertical"
-              android:layout_margin="20dp">
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="fill_parent"
+    android:layout_height="wrap_content"
+    android:background="@drawable/section"
+    android:orientation="vertical"
+    android:layout_margin="20dp">
 
     <TextView
         android:id="@+id/textView1"
@@ -24,7 +25,7 @@
     <LinearLayout
         android:orientation="vertical"
         android:layout_width="match_parent"
-        android:layout_height="match_parent"
+        android:layout_height="wrap_content"
         android:layout_margin="8dp"
         android:padding="7dp"
         android:background="@drawable/entry">
@@ -32,10 +33,10 @@
         <ProgressBar
             android:id="@+id/pb_sync_dialog"
             style="?android:attr/progressBarStyleLarge"
-            android:layout_width="match_parent"
-            android:layout_height="wrap_content"
+            android:layout_width="wrap_content"
             android:layout_gravity="center_horizontal"
-            android:layout_marginTop="20dp"/>
+            android:layout_marginTop="20dp"
+            android:layout_height="60dp"/>
 
         <TextView
             android:id="@+id/tv_dialog_sync_text"

+ 21 - 21
app/src/main/res/layout/dialog_sync_failed.xml

@@ -4,31 +4,31 @@
     android:layout_height="wrap_content"
     android:background="@drawable/section"
     android:orientation="vertical"
-    android:paddingBottom="10dp" >
+    android:layout_margin="20dp">
 
-    <LinearLayout
+    <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
-        android:orientation="vertical"
-        android:padding="7dp">
-
-        <TextView
-            android:layout_width="fill_parent"
-            android:layout_height="wrap_content"
-            android:text="@string/dialog_sync_failed_title"
-            android:textAppearance="@android:style/TextAppearance.Large"
-            android:layout_marginBottom="10dp"
-            android:textStyle="bold"/>
-
-        <TextView
-            android:layout_width="fill_parent"
-            android:layout_height="wrap_content"
-            android:text="@string/dialog_sync_failed_text"
-            android:background="@drawable/entry"
-            android:padding="7dp"
-            android:textAppearance="@android:style/TextAppearance.Medium"/>
+        android:text="@string/dialog_sync_failed_title"
+        android:textAppearance="?android:attr/textAppearanceLarge"
+        android:textStyle="bold"
+        android:background="@drawable/section_title"
+        android:paddingBottom="4dp"
+        android:paddingEnd="10dp"
+        android:paddingLeft="20dp"
+        android:paddingRight="10dp"
+        android:paddingStart="20dp"
+        android:paddingTop="4dp"
+        android:textColor="@color/section_title"/>
 
-    </LinearLayout>
+    <TextView
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:textAppearance="@android:style/TextAppearance.Medium"
+        android:text="@string/dialog_sync_failed_text"
+        android:layout_margin="8dp"
+        android:padding="7dp"
+        android:background="@drawable/entry"/>
 
     <Button
         android:id="@+id/bt_dialog_sync_failed_close"