Browse Source

Location permissions asked on runtime (once every time the app starts).
La Blanca section for the home screen implemented.

seavenois 10 năm trước cách đây
mục cha
commit
1558e6f80d

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

@@ -20,7 +20,12 @@ final class GM {
 	 * Address where the web server is
 	 */
 	static final String SERVER = "http://margolariak.es";
-	
+
+	/**
+	 * Code for the location permission request
+	 */
+	static final int PERMISSION_LOCATION = 1;
+
 	/**
 	 * Duration of the menu sliding animation.
 	 */

+ 65 - 2
app/src/main/java/com/ivalentin/margolariak/HomeLayout.java

@@ -7,6 +7,7 @@ import java.util.Calendar;
 import java.util.Locale;
 
 import android.annotation.SuppressLint;
+import android.Manifest;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
@@ -74,6 +75,11 @@ public class HomeLayout extends Fragment implements LocationListener {
 		//Variable to know if I need a location manager
 		boolean requestLocation = false;
 
+		//Request location permissions if not set
+		if (ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
+			ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, GM.PERMISSION_LOCATION);
+		}
+
 		//Set Location manager
 		//TODO: Only do this if location is required
 		locationManager = (LocationManager) view.getContext().getSystemService(Context.LOCATION_SERVICE);
@@ -379,9 +385,66 @@ public class HomeLayout extends Fragment implements LocationListener {
 	 *
 	 * @return True if the section has been shown, false otherwise.
 	 */
+	@SuppressWarnings("ResultOfMethodCallIgnored")
 	private boolean setUpLablanca(View view) {
-		//TODO: setUpLablanca(View view)
-		return false;
+		SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
+		Boolean set = false;
+		if (preferences.getInt(GM.PREF_DB_FESTIVALS, 0) == 1) {
+			LinearLayout llSection = (LinearLayout) view.findViewById(R.id.ll_home_section_lablanca);
+			TextView tvText = (TextView) view.findViewById(R.id.tv_home_section_lablanca_text);
+			ImageView ivImage = (ImageView) view.findViewById(R.id.iv_home_section_lablanca_image);
+
+			//Get data from the database of the future activities
+			SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
+			int year = Calendar.getInstance().get(Calendar.YEAR);
+			String lang = GM.getLang();
+			Cursor cursor = db.rawQuery("SELECT text_" + lang + ", img FROM festival WHERE year = " + year + ";", null);
+			if (cursor.getCount() > 0) {
+				cursor.moveToFirst();
+				llSection.setVisibility(View.VISIBLE);
+
+				//Set text
+				String text = Html.fromHtml(cursor.getString(0)).toString();
+				String shortText = text.substring(0, 80);
+				if (text.equals(shortText)) {
+					tvText.setText(text);
+				}
+				else{
+					shortText = shortText + "...";
+					tvText.setText(shortText);
+				}
+
+				set = true;
+
+				//Set image
+				String image = cursor.getString(1);
+				if (image.length() > 0){
+
+					//Check if image exists
+					File f;
+					f = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image);
+					if (f.exists()) {
+						//If the image exists, set it.
+						Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image);
+						ivImage.setImageBitmap(myBitmap);
+					} else {
+						//If not, create directories and download asynchronously
+						File fpath;
+						fpath = new File(this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/");
+						fpath.mkdirs();
+						new DownloadImage(GM.SERVER + "/img/fiestas/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/fiestas/preview/" + image, ivImage).execute();
+					}
+				}
+				else {
+					ivImage.setVisibility(View.GONE);
+				}
+			}
+			cursor.close();
+			db.close();
+
+
+		}
+		return set;
 	}
 
 	/**

+ 28 - 10
app/src/main/res/layout/fragment_layout_home.xml

@@ -72,33 +72,51 @@
 		</LinearLayout>
 
 		<LinearLayout
-			android:id="@+id/ll_home_section_lablanca"
-			android:layout_width="fill_parent"
-			android:layout_height="wrap_content"
 			android:orientation="vertical"
+			android:layout_width="match_parent"
+			android:layout_height="match_parent"
 			android:background="@drawable/section"
-			android:layout_margin="7dp"
 			android:padding="7dp"
+			android:id="@+id/ll_home_section_lablanca"
+			android:layout_margin="7dp"
 			android:visibility="gone">
 
 			<TextView
 				android:layout_width="wrap_content"
 				android:layout_height="wrap_content"
-				android:text="@string/home_section_lablanca"
 				android:textAppearance="?android:attr/textAppearanceLarge"
+				android:text="@string/lablanca_title"
+				android:id="@+id/textView4"
 				android:textStyle="bold"/>
 
 			<LinearLayout
-				android:id="@+id/ll_home_section_lablanca_content"
+				android:orientation="horizontal"
 				android:layout_width="match_parent"
-				android:layout_height="wrap_content"
-				android:layout_margin="10dp"
-				android:orientation="vertical"
+				android:layout_height="match_parent"
 				android:background="@drawable/entry"
 				android:padding="7dp">
 
-			</LinearLayout>
+				<ImageView
+					android:layout_width="wrap_content"
+					android:layout_height="wrap_content"
+					android:id="@+id/iv_home_section_lablanca_image"
+					android:contentDescription="@string/app_cdesc"
+					android:layout_weight=".4"
+					android:layout_margin="3dp"
+					android:background="@color/album_border"
+					android:padding="1dp"/>
 
+				<TextView
+					android:layout_width="wrap_content"
+					android:layout_height="wrap_content"
+					android:textAppearance="@android:style/TextAppearance.Medium"
+					android:id="@+id/tv_home_section_lablanca_text"
+					android:layout_weight=".6"
+					android:padding="7dp"
+					android:maxLines="4"
+					android:ellipsize="end"/>
+
+			</LinearLayout>
 		</LinearLayout>
 
 		<LinearLayout