Просмотр исходного кода

Maps for future activity schedules.

seavenois 10 лет назад
Родитель
Сommit
793064524f

+ 294 - 2
app/src/main/java/com/ivalentin/margolariak/ActivityFutureLayout.java

@@ -1,7 +1,9 @@
 package com.ivalentin.margolariak;
 
 import android.annotation.SuppressLint;
+import android.app.Dialog;
 import android.content.Context;
+import android.content.DialogInterface;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.graphics.Bitmap;
@@ -9,15 +11,32 @@ import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.app.Fragment;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.Window;
+import android.view.WindowManager;
 import android.webkit.WebView;
+import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
+import com.google.android.gms.maps.CameraUpdate;
+import com.google.android.gms.maps.CameraUpdateFactory;
+import com.google.android.gms.maps.GoogleMap;
+import com.google.android.gms.maps.MapView;
+import com.google.android.gms.maps.MapsInitializer;
+import com.google.android.gms.maps.OnMapReadyCallback;
+import com.google.android.gms.maps.model.LatLng;
+import com.google.android.gms.maps.model.MarkerOptions;
+
 import java.io.File;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
 import java.util.Locale;
 
 /**
@@ -30,7 +49,15 @@ import java.util.Locale;
  * @see Fragment
  *
  */
-public class ActivityFutureLayout extends Fragment {
+public class ActivityFutureLayout extends Fragment implements OnMapReadyCallback {
+
+	//Map stuff for the dialog
+	private MapView mapView;
+	private GoogleMap map;
+	private LatLng location;
+	private View view;
+	private String markerName = "";
+	private Bundle bund;
 
 	@SuppressWarnings("ResultOfMethodCallIgnored")
     @SuppressLint("InflateParams")
@@ -131,6 +158,10 @@ public class ActivityFutureLayout extends Fragment {
                 TextView tvSchTitle = (TextView) entry.findViewById(R.id.tv_row_activity_itinerary_title);
                 tvSchTitle.setText(cursorItinerary.getString(1));
 
+				//Set Id
+				TextView tvSchId = (TextView) entry.findViewById(R.id.tv_row_activity_itinerary_id);
+				tvSchId.setText(cursorItinerary.getString(0));
+
                 //Set description
                 TextView tvSchDescription = (TextView) entry.findViewById(R.id.tv_row_activity_itinerary_description);
                 if (!cursorItinerary.getString(1).equals(cursorItinerary.getString(2))){
@@ -153,7 +184,15 @@ public class ActivityFutureLayout extends Fragment {
                     tvSchAddress.setText(cursorItinerary.getString(5));
                 }
 
-                //TODO: Add an onclick listener on image that shows a map
+				//Set clisk listener
+				entry.setOnClickListener(new View.OnClickListener() {
+					@Override
+					public void onClick(View v) {
+						TextView tvId = (TextView) v.findViewById(R.id.tv_row_activity_itinerary_id);
+						int id = Integer.parseInt(tvId.getText().toString());
+						showDialog(id);
+					}
+				});
 
                 list.addView(entry);
             }
@@ -164,4 +203,257 @@ public class ActivityFutureLayout extends Fragment {
         db.close();
         return view;
     }
+
+    /**
+     * Shows a dialog with info about the selected event.
+     *
+     * @param id The event id
+     */
+    private void showDialog(final int id){
+
+        //Create the dialog
+        final Dialog dialog = new Dialog(getActivity());
+
+        //Set up dialog window
+        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
+        dialog.setContentView(R.layout.dialog_schedule);
+
+        //Date formatters
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
+        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd-", Locale.US);
+        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.US);
+
+        //Set the custom dialog components - text, image and button
+        TextView tvTitle = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_title);
+        TextView tvDescription = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_description);
+        TextView tvHost = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_host);
+        TextView tvDate = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_date);
+        TextView tvTime = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_time);
+        TextView tvPlace = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_place);
+        TextView tvAddress = (TextView) dialog.findViewById(R.id.tv_dialog_schedule_address);
+        Button btClose = (Button) dialog.findViewById(R.id.bt_schedule_close);
+
+        //Get info about the event
+        SQLiteDatabase db = getActivity().openOrCreateDatabase(GM.DB_NAME, Context.MODE_PRIVATE, null);
+        String lang = GM.getLang();
+        Cursor cursor = db.rawQuery("SELECT activity_itinerary.id, activity_itinerary.name_" + lang + ", description_" + lang + ", place, start, end, place.name_" + lang + ", address_" + lang + ", lat, lon FROM activity_itinerary, place WHERE place = place.id AND activity_itinerary.id = " + id + ";", null);
+        if (cursor.getCount() > 0){
+            cursor.moveToNext();
+
+            //Set title
+            tvTitle.setText(cursor.getString(1));
+            markerName = cursor.getString(1);
+
+            //Set description
+            tvDescription.setText(cursor.getString(2));
+
+            //Set host field hidden
+           	tvHost.setVisibility(View.GONE);
+
+            //Set date
+            try{
+                Date day = dateFormat.parse(cursor.getString(4));
+                Date date = new Date();
+                //If the event is today, show "Today" instead of the date
+                if (dayFormat.format(day).equals(dayFormat.format(date)))
+                    tvDate.setText(dialog.getContext().getString(R.string.today));
+
+                else{
+                    Calendar cal = Calendar.getInstance();
+                    cal.setTime(date);
+                    cal.add(Calendar.HOUR_OF_DAY, 24);
+
+                    //If the event is tomorrow, show "Tomorrow" instead of the date
+                    if (dayFormat.format(cal.getTime()).equals(dayFormat.format(date))){
+                        tvDate.setText(dialog.getContext().getString(R.string.tomorrow));
+                    }
+
+                    //Else, show the date
+                    else{
+                        SimpleDateFormat printFormat = new SimpleDateFormat("dd MMMM", Locale.US);
+                        tvDate.setText(printFormat.format(day));
+                    }
+                }
+            }
+            catch (Exception ex){
+                Log.e("Error parsing date", ex.toString());
+            }
+
+            //Set time
+            try{
+                if (cursor.getString(5).length() == 0) {
+                    tvTime.setText(timeFormat.format(dateFormat.parse(cursor.getString(4))));
+                }
+                else {
+                    String time = timeFormat.format(dateFormat.parse(cursor.getString(4))) + " - " + timeFormat.format(dateFormat.parse(cursor.getString(5)));
+                    tvTime.setText(time);
+                }
+            }
+            catch (ParseException ex){
+                Log.e("Error parsing time", ex.toString());
+            }
+
+            //Set the place
+            tvPlace.setText(cursor.getString(6));
+            tvAddress.setText(cursor.getString(7));
+
+            //Set up map
+            location = new LatLng(Double.parseDouble(cursor.getString(8)), Double.parseDouble(cursor.getString(9)));
+            mapView = (MapView) dialog.findViewById(R.id.mv_dialog_schedule_map);
+            mapView.onCreate(bund);
+
+            //Close the db connection
+            cursor.close();
+            db.close();
+
+            //Set close button
+            btClose.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    dialog.dismiss();
+                }
+            });
+
+            //Actions to take when the dialog is cancelled
+            dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
+                @Override
+                public void onCancel(DialogInterface dialog) {
+                    if (map != null)
+                        map.setMyLocationEnabled(false);
+                    if (mapView != null){
+                        mapView.onResume();
+                        mapView.onDestroy();
+                    }
+                }
+            });
+
+            //Show the dialog
+            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
+            lp.copyFrom(dialog.getWindow().getAttributes());
+            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
+            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
+            lp.gravity = Gravity.CENTER;
+            lp.dimAmount = 0.0f;
+            dialog.getWindow().setAttributes(lp);
+
+            //Show dialog
+            dialog.show();
+
+            //Start the map
+            startMap();
+            mapView.onResume();
+            dialog.setOnShowListener(new DialogInterface.OnShowListener(){
+
+                @Override
+                public void onShow(DialogInterface dialog) {
+                    // Gets to GoogleMap from the MapView and does initialization stuff
+                    startMap();
+
+                }
+            });
+
+        }
+    }
+
+	/**
+	 * Starts the map in the dialog.
+	 */
+	private void startMap(){
+		mapView.getMapAsync(this);
+	}
+
+	/**
+	 * Called when the fragment is brought back into the foreground.
+	 * Resumes the map and the location manager.
+	 *
+	 * @see android.app.Fragment#onResume()
+	 */
+	@Override
+	public void onResume() {
+		if (mapView != null)
+			mapView.onResume();
+		if (map != null)
+			map.setMyLocationEnabled(true);
+		super.onResume();
+	}
+
+	/**
+	 * Called when the fragment is destroyed.
+	 * Finishes the map.
+	 *
+	 * @see android.app.Fragment#onDestroy()
+	 */
+	@Override
+	public void onDestroy() {
+		super.onDestroy();
+		if (map != null)
+			map.setMyLocationEnabled(false);
+		if (mapView != null){
+			mapView.onResume();
+			mapView.onDestroy();
+		}
+	}
+
+	/**
+	 * Called when the fragment is paused.
+	 * Finishes the map.
+	 *
+	 * @see android.app.Fragment#onPause()
+	 */
+	@Override
+	public void onPause() {
+		super.onDestroy();
+		if (map != null) {
+			map.setMyLocationEnabled(false);
+		}
+		if (mapView != null){
+			mapView.onPause();
+		}
+	}
+
+	/**
+	 * Called in a situation of low memory.
+	 * Lets the map handle this situation.
+	 *
+	 * @see android.app.Fragment#onLowMemory()
+	 */
+	@Override
+	public void onLowMemory() {
+		super.onLowMemory();
+		if (mapView != null){
+			mapView.onResume();
+			mapView.onLowMemory();
+		}
+	}
+
+	/**
+	 * Called when the map is ready to be displayed.
+	 * Sets the map options and a marker for the map.
+	 *
+	 * @param googleMap The map to be shown
+	 *
+	 * @see com.google.android.gms.maps.OnMapReadyCallback#onMapReady(com.google.android.gms.maps.GoogleMap)
+	 */
+	@Override
+	public void onMapReady(GoogleMap googleMap) {
+		this.map = googleMap;
+		map.setMyLocationEnabled(true);
+
+		map.getUiSettings().setMyLocationButtonEnabled(false);
+		map.setMyLocationEnabled(true);
+		// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
+		try {
+			MapsInitializer.initialize(this.getActivity());
+			CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(location, 14);
+			map.animateCamera(cameraUpdate);
+		} catch (Exception e) {
+			Log.e("Error initializing maps", e.toString());
+		}
+		//Set GM marker
+		MarkerOptions mo = new MarkerOptions();
+		mo.title(markerName);
+		mo.position(location);
+		map.addMarker(mo);
+
+	}
 }

+ 4 - 16
app/src/main/java/com/ivalentin/margolariak/HomeLayout.java

@@ -59,7 +59,7 @@ public class HomeLayout extends Fragment implements LocationListener{
 	 * 
 	 * @return The fragment view
 	 * 
-	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
+	 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
 	 */
 	@SuppressLint("InflateParams") //Throws unknown error when done properly.
 	@Override
@@ -73,14 +73,12 @@ public class HomeLayout extends Fragment implements LocationListener{
 		locationManager = (LocationManager) view.getContext().getSystemService(Context.LOCATION_SERVICE);
 		locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), GM.LOCATION_ACCURACY_TIME, GM.LOCATION_ACCURACY_SPACE, this);
 		onLocationChanged(locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
-		//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 5, this);
-		
+
 		//Set the title
 		((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_home));
 
-		//TODO: These methods are empty yet
+		//TODO: This methods is empty
 		setUpLablanca(view);
-		setUpAround(view);
 
 		//Set up the blog section
 		setUpBlog(view);
@@ -350,7 +348,7 @@ public class HomeLayout extends Fragment implements LocationListener{
 			}
 		});
 
-		googleplus.setOnClickListener(new View.OnClickListener() {
+		youtube.setOnClickListener(new View.OnClickListener() {
 			@Override
 			public void onClick(View v) {
 				String url = v.getContext().getString(R.string.social_youtube_link);
@@ -371,16 +369,6 @@ public class HomeLayout extends Fragment implements LocationListener{
 		});
 	}
 
-	/**
-	 * Populates the Around section of the home screen.
-	 *
-	 * @return The number of entries shown.
-	 */
-	private int setUpAround(View view) {
-		//TODO: setUpAround(View view)
-		return 0;
-	}
-
 	/**
 	 * Populates the La Blanca section of the home screen.
 	 *

+ 8 - 0
app/src/main/res/layout/row_activity_schedule.xml

@@ -16,6 +16,14 @@
         android:textStyle="bold"
         android:gravity="end" />
 
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:text="Medium Text"
+        android:id="@+id/tv_row_activity_itinerary_id"
+        android:visibility="gone"/>
+
     <RelativeLayout
         android:id="@+id/relativeLayout"
         android:layout_width="20dp"