| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110 |
- package com.ivalentin.margolariak;
- import java.io.File;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- 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;
- import android.content.pm.PackageManager;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.app.Fragment;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.net.Uri;
- import android.os.Bundle;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.support.v4.app.ActivityCompat;
- import android.text.Html;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.MenuItem;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- /**
- * Section that will be seen when the app is started.
- * Contains info from almost every other section.
- *
- * @author Iñigo Valentin
- *
- * @see Fragment
- *
- */
- @SuppressWarnings("UnusedReturnValue")
- public class HomeLayout extends Fragment implements LocationListener {
- //The location manager
- private LocationManager locationManager;
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // TODO Auto-generated method stub
- Log.e("MENU", "FROM HOME");
- return false;
- }
- private View view;
- /**
- * Run when the fragment is inflated.
- * Assigns views, gets the date and does the first call to the {//@link populate function}.
- *
- * @param inflater A LayoutInflater to handle the views
- * @param container The parent View
- * @param savedInstanceState Bundle with the saved state
- *
- * @return The fragment view
- *
- * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
- */
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- @Override
- public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
- //Load the layout.
- view = inflater.inflate(R.layout.fragment_layout_home, null);
- //Variable to know if I need a location manager
- //boolean requestLocation = false;
- //Show tutorial if its the first time
- /*final SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
- if (!preferences.getBoolean(GM.PREF_TUTORIAL, false)) {
- final RelativeLayout llTutorial = (RelativeLayout) getActivity().findViewById(R.id.rl_tutorial);
- llTutorial.setVisibility(View.VISIBLE);
- llTutorial.setOnTouchListener(new View.OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- return true;
- }
- });
- Button btTutorial = (Button) getActivity().findViewById(R.id.bt_tutorial);
- btTutorial.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- llTutorial.setVisibility(View.GONE);
- SharedPreferences.Editor editor = preferences.edit();
- editor.putBoolean(GM.PREF_TUTORIAL, true);
- editor.apply();
- }
- });
- }*/
- //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);
- if (!(ActivityCompat.checkSelfPermission(view.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(view.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
- locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), GM.LOCATION_ACCURACY_TIME, GM.LOCATION_ACCURACY_SPACE, this);
- onLocationChanged(locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER));
- }
- //Set the title
- ((MainActivity) getActivity()).setSectionTitle(view.getContext().getString(R.string.menu_home));
- setUpLablanca(view);
- //Set up the blog section
- setUpBlog(view);
- //Set up activities sections
- if (setUpFutureActivities(view) == 0) {
- setUpPastActivities(view);
- }
- //Set up the gallery section
- setUpGallery(view);
- //Asynchronously set up location section
- //This will trigger onLocationChanged, that will trigger setUpLocation
- new HomeSectionLocation(view, this.getActivity()).execute();
- //Set up schedule sections
- setUpSchedule(1, view);
- setUpSchedule(0, view);
- //Setup the social section
- setUpSocial(view);
- //Return the view itself.
- return view;
- }
- /**
- * Populates the schedule sections of the home screen.
- *
- * @return The number of entries shown.
- */
- @SuppressLint("InflateParams")
- private int setUpSchedule(int gm, View view) {
- int count = 0;
- SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
- if (preferences.getInt(GM.PREF_DB_FESTIVALS, 0) == 0) {
- return count;
- }
- SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
- Calendar calendar = Calendar.getInstance();
- Calendar calendarEnd;
- DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
- String nowString = dateFormat.format(calendar.getTime());
- String endString;
- String query;
- //An inflater
- LayoutInflater factory = LayoutInflater.from(getActivity());
- //Views in each row
- TextView tvRowTitle, tvRowTime, tvRowPlace, tvRowId;
- //Assign layouts
- LinearLayout section, listNow, listNext, now, next, entry;
- if (gm == 1) {
- section = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule);
- listNow = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_now_list);
- listNext = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_later_list);
- now = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_now_content);
- next = (LinearLayout) view.findViewById(R.id.ll_home_section_gmschedule_later_content);
- } else {
- section = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule);
- listNow = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_now_list);
- listNext = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_later_list);
- now = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_now_content);
- next = (LinearLayout) view.findViewById(R.id.ll_home_section_cityschedule_later_content);
- }
- //Set query for current events
- String lang = GM.getLang();
- calendarEnd = calendar;
- calendarEnd.add(Calendar.MINUTE, -40);
- endString = dateFormat.format(calendarEnd.getTime());
- query = "SELECT festival_event.id, title_" + lang + ", description_" + lang + ", place, start, end, name_" + lang + ", address_" + lang + ", lat, lon FROM festival_event, place WHERE place = place.id AND ((start <= '" + nowString + "' AND end > '" + nowString + "') OR (start > '" + endString + "' AND start < '" + nowString + "')) AND gm = " + gm + " ORDER BY start LIMIT 2";
- //Execute query and loop
- Cursor cursorNow = db.rawQuery(query, null);
- if (cursorNow.getCount() == 0) {
- now.setVisibility(View.GONE);
- }
- while (cursorNow.moveToNext()) {
- count++;
- entry = (LinearLayout) factory.inflate(R.layout.row_home_schedule, null);
- //Set id
- tvRowId = (TextView) entry.findViewById(R.id.tv_row_home_schedule_id);
- tvRowId.setText(cursorNow.getString(0));
- //Set title
- tvRowTitle = (TextView) entry.findViewById(R.id.tv_row_home_schedule_title);
- tvRowTitle.setText(cursorNow.getString(1));
- //Set place
- tvRowPlace = (TextView) entry.findViewById(R.id.tv_row_home_schedule_place);
- tvRowPlace.setText(cursorNow.getString(6));
- //Set icon
- if (gm == GM.SECTION_LABLANCA_GM_SCHEDULE) {
- ImageView pinPoint = (ImageView) entry.findViewById(R.id.iv_row_home_schedule_pinpoint);
- pinPoint.setImageResource(getResources().getIdentifier("com.ivalentin.gm:drawable/pinpoint_gm", null, null));
- }
- //Set time
- tvRowTime = (TextView) entry.findViewById(R.id.tv_row_home_schedule_time);
- String tm = cursorNow.getString(4).substring(cursorNow.getString(4).length() - 8, cursorNow.getString(4).length() - 3);
- tvRowTime.setText(tm);
- //Add the view
- listNow.addView(entry);
- }
- cursorNow.close();
- //Prepare query for upcaming events
- calendarEnd = calendar;
- calendarEnd.add(Calendar.MINUTE, 320);
- endString = dateFormat.format(calendarEnd.getTime());
- query = "SELECT festival_event.id, title_" + lang + ", description_" + lang + ", place, start, end, name_" + lang + ", address_" + lang + ", lat, lon FROM festival_event, place WHERE place = place.id AND start > '" + nowString + "' AND start < '" + endString + "' AND gm = " + gm + " ORDER BY start LIMIT 2";
- //Execute query and loop
- Cursor cursorNext = db.rawQuery(query, null);
- if (cursorNext.getCount() == 0) {
- next.setVisibility(View.GONE);
- }
- while (cursorNext.moveToNext()) {
- count++;
- entry = (LinearLayout) factory.inflate(R.layout.row_home_schedule, null);
- //Set id
- tvRowId = (TextView) entry.findViewById(R.id.tv_row_home_schedule_id);
- tvRowId.setText(cursorNext.getString(0));
- //Set title
- tvRowTitle = (TextView) entry.findViewById(R.id.tv_row_home_schedule_title);
- tvRowTitle.setText(cursorNext.getString(1));
- //Set place
- tvRowPlace = (TextView) entry.findViewById(R.id.tv_row_home_schedule_place);
- tvRowPlace.setText(cursorNext.getString(6));
- //Set icon
- if (gm == GM.SECTION_LABLANCA_GM_SCHEDULE) {
- ImageView pinPoint = (ImageView) entry.findViewById(R.id.iv_row_home_schedule_pinpoint);
- pinPoint.setImageResource(getResources().getIdentifier("com.ivalentin.gm:drawable/pinpoint_gm", null, null));
- }
- //Set time
- tvRowTime = (TextView) entry.findViewById(R.id.tv_row_home_schedule_time);
- String tm = cursorNext.getString(4).substring(cursorNext.getString(4).length() - 8, cursorNext.getString(4).length() - 3);
- tvRowTime.setText(tm);
- //Add the view
- listNext.addView(entry);
- }
- cursorNext.close();
- if (count > 0) {
- section.setVisibility(View.VISIBLE);
- if (gm == 1) {
- section.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_LABLANCA_GM_SCHEDULE, false);
- }
- });
- } else {
- section.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_LABLANCA_SCHEDULE, false);
- }
- });
- }
- }
- db.close();
- return count;
- }
- /**
- * Populates the Social section of the home screen.
- */
- private void setUpSocial(View view) {
- ImageView phone = (ImageView) view.findViewById(R.id.iv_home_section_social_phone);
- ImageView mail = (ImageView) view.findViewById(R.id.iv_home_section_social_mail);
- ImageView whatsapp = (ImageView) view.findViewById(R.id.iv_home_section_social_whatsapp);
- ImageView facebook = (ImageView) view.findViewById(R.id.iv_home_section_social_facebook);
- ImageView twitter = (ImageView) view.findViewById(R.id.iv_home_section_social_twitter);
- ImageView googleplus = (ImageView) view.findViewById(R.id.iv_home_section_social_googleplus);
- ImageView youtube = (ImageView) view.findViewById(R.id.iv_home_section_social_youtube);
- ImageView instagram = (ImageView) view.findViewById(R.id.iv_home_section_social_instagram);
- phone.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_phone_link);
- Intent intent = new Intent(Intent.ACTION_DIAL);
- intent.setData(Uri.parse(url));
- startActivity(intent);
- }
- });
- mail.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_mail_link);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- }
- });
- whatsapp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- try {
- Uri mUri = Uri.parse(getString(R.string.social_whatsapp_link));
- Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
- mIntent.setPackage(getString(R.string.social_whatsapp_package));
- mIntent.putExtra(getString(R.string.social_whatsapp_chat), true);
- startActivity(mIntent);
- } catch (Exception e) {
- Toast.makeText(v.getContext(), getString(R.string.social_whatsapp_error), Toast.LENGTH_LONG).show();
- }
- }
- });
- facebook.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_facebook_link);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- }
- });
- twitter.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_twitter_link);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- }
- });
- googleplus.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_googleplus_link);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- }
- });
- youtube.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_youtube_link);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- }
- });
- instagram.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = v.getContext().getString(R.string.social_instagram_link);
- Intent i = new Intent(Intent.ACTION_VIEW);
- i.setData(Uri.parse(url));
- startActivity(i);
- }
- });
- }
- /**
- * Populates the La Blanca section of the home screen.
- *
- * @return True if the section has been shown, false otherwise.
- */
- @SuppressWarnings("ResultOfMethodCallIgnored")
- private boolean setUpLablanca(View view) {
- 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);
- //Set click listener
- llSection.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_LABLANCA, false);
- }
- });
- //Get data from the database of the future activities
- SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
- 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);
- //Enable menu entries
- //this.getActivity().findViewById(R.id.menu_lablanca_gm_schedule).setVisibility(View.VISIBLE);
- //this.getActivity().findViewById(R.id.menu_lablanca_schedule).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, GM.IMG_PREVIEW).execute();
- }
- }
- else {
- ivImage.setVisibility(View.GONE);
- }
- }
- cursor.close();
- db.close();
- }
- return set;
- }
- /**
- * Populates the Location section of the home screen.
- *
- * @return True if the section has been shown, false otherwise.
- */
- private boolean setUpLocation(Location location, View view) {
- SharedPreferences preferences = view.getContext().getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
- if (preferences.getString(GM.PREF_GM_LOCATION, GM.DEFAULT_PREF_GM_LOCATION).equals(GM.DEFAULT_PREF_GM_LOCATION) == false) {
- try {
- Double lat = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LATITUDE, 0));
- Double lon = Double.longBitsToDouble(preferences.getLong(GM.PREF_GM_LONGITUDE, 0));
- Double distance = Distance.calculateDistance(lat, lon, location.getLatitude(), location.getLongitude());
- TextView text = (TextView) view.findViewById(R.id.tv_home_section_location_distance);
- if (distance <= 2) {
- distance = 1000 * distance;
- text.setText(String.format(getString(R.string.home_section_location_text_short), distance.intValue(), (int) (0.012 * distance.intValue())));
- } else {
- text.setText(String.format(getString(R.string.home_section_location_text_long), String.format(Locale.US, "%.02f", distance)));
- }
- return true;
- }
- catch (Exception ex){
- Log.e("Location", "Couldn't set up home location section: " + ex.toString());
- }
- }
- return false;
- }
- /**
- * Populates the Gallery section of the home screen.
- * Loads four photos.
- * Tapping them takes to the respective album, tapping anywhere else, to the gallery section.
- *
- * @return The number of entries shown
- */
- @SuppressWarnings("ResultOfMethodCallIgnored")
- private int setUpGallery(View view) {
- int counter = 0;
- //Set onClick listener of section
- LinearLayout llGallery = (LinearLayout) view.findViewById(R.id.ll_home_section_gallery);
- //Set click listener
- llGallery.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_GALLERY, false);
- }
- });
- //Assign views
- ImageView ivPhoto[] = new ImageView[4];
- ivPhoto[0] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_0);
- ivPhoto[1] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_1);
- ivPhoto[2] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_2);
- ivPhoto[3] = (ImageView) view.findViewById(R.id.iv_home_section_gallery_3);
- //Get data from the database of the future activities
- SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
- Cursor cursor = db.rawQuery("SELECT id, album, file, uploaded FROM photo, photo_album WHERE id = photo ORDER BY uploaded DESC LIMIT 4;", null);
- while (cursor.moveToNext()) {
- //Set id
- ivPhoto[counter].setTag(R.string.key_0, cursor.getString(0));
- ivPhoto[counter].setTag(R.string.key_1, cursor.getString(1));
- //Set image
- String image = cursor.getString(2);
- //Check if image exists
- File f;
- f = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
- if (f.exists()) {
- //If the image exists, set it.
- Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image);
- ivPhoto[counter].setImageBitmap(myBitmap);
- } else {
- //If not, create directories and download asynchronously
- File fpath;
- fpath = new File(this.getActivity().getFilesDir().toString() + "/img/galeria/preview/");
- fpath.mkdirs();
- new DownloadImage(GM.SERVER + "/img/galeria/preview/" + image, this.getActivity().getFilesDir().toString() + "/img/galeria/preview/" + image, ivPhoto[counter], GM.IMG_PREVIEW).execute();
- }
- //Set listeners for images
- ivPhoto[counter].setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment fragment = new AlbumLayout();
- Bundle bundle = new Bundle();
- int photoId = Integer.parseInt((String) v.getTag(R.string.key_0));
- int albumId = Integer.parseInt((String) v.getTag(R.string.key_1));
- bundle.putInt("album", albumId);
- bundle.putInt("photo", photoId);
- fragment.setArguments(bundle);
- FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- ft.replace(R.id.activity_main_content_fragment, fragment);
- ft.addToBackStack("album_" + albumId);
- ft.commit();
- }
- });
- counter++;
- }
- //Close db
- cursor.close();
- db.close();
- return counter;
- }
- /**
- * Populates the Past Activities section of the home screen.
- * Makes the section visible and adds two rows with the two last activities.
- * Tapping them takes to the post, tapping anywhere else, to the blog section.
- *
- * @return The number of entries shown
- */
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- @SuppressWarnings("ResultOfMethodCallIgnored")
- private int setUpPastActivities(View view) {
- int counter = 0;
- String lang = GM.getLang();
- //Get data from the database of the future activities
- SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
- Cursor cursor = db.rawQuery("SELECT id, date, city, title_" + lang + " AS title, text_" + lang + " AS text, price, after_" + lang + " AS after FROM activity WHERE date < date('now') ORDER BY date DESC LIMIT 2;", null);
- //Show section
- LinearLayout llActivitiesPast = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_past);
- llActivitiesPast.setVisibility(View.VISIBLE);
- //Set click listener
- llActivitiesPast.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_ACTIVITIES, false);
- }
- });
- LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_past_content);
- LinearLayout entry;
- //An inflater
- LayoutInflater factory = LayoutInflater.from(getActivity());
- //Print rows
- while (cursor.moveToNext()) {
- //Create a new row
- entry = (LinearLayout) factory.inflate(R.layout.row_home_activities, null);
- //Set margins
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- layoutParams.setMargins(10, 10, 10, 25);
- entry.setLayoutParams(layoutParams);
- //Set title
- TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_home_activity_title);
- tvTitle.setText(cursor.getString(3));
- //Set city
- TextView tvCity = (TextView) entry.findViewById(R.id.tv_row_home_activity_city);
- tvCity.setText(cursor.getString(2));
- //Hide price
- TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_home_activity_price);
- tvPrice.setVisibility(View.GONE);
- //tvPrice.setText(cursor.getString(5) + "€");
- //Set text
- String text;
- if (cursor.getString(6).length() < 1) {
- text = Html.fromHtml(cursor.getString(4)).toString();
- } else {
- text = Html.fromHtml(cursor.getString(6)).toString();
- }
- if (text.length() > 100) {
- text = text.substring(0, 100) + "...";
- }
- TextView tvText = (TextView) entry.findViewById(R.id.tv_row_home_activity_text);
- tvText.setText(text);
- //Set date
- TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_home_activity_date);
- tvDate.setText(GM.formatDate(cursor.getString(1) + " 00:00:00", lang, false));
- //Set hidden id
- TextView tvId = (TextView) entry.findViewById(R.id.tv_row_home_activity_hidden);
- tvId.setText(cursor.getString(0));
- //Get image
- ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_home_activity);
- Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
- if (cursorImage.getCount() > 0) {
- cursorImage.moveToFirst();
- String image = cursorImage.getString(0);
- //Check if image exists
- File f;
- f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
- if (f.exists()) {
- //If the image exists, set it.
- Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
- iv.setImageBitmap(myBitmap);
- } else {
- //If not, create directories and download asynchronously
- File fpath;
- fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
- fpath.mkdirs();
- new DownloadImage(GM.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG_MINIATURE).execute();
- }
- }
- cursorImage.close();
- //Set onCLickListener
- entry.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //Toast.makeText(getActivity(), "OPENING POST", Toast.LENGTH_LONG).show();
- Fragment fragment = new ActivityPastLayout();
- Bundle bundle = new Bundle();
- //Pass post id
- int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_home_activity_hidden)).getText().toString());
- bundle.putInt("activity", id);
- fragment.setArguments(bundle);
- FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- ft.replace(R.id.activity_main_content_fragment, fragment);
- ft.addToBackStack("activity_" + id);
- ft.commit();
- }
- });
- //Add to the list
- llList.addView(entry);
- counter++;
- }
- //Close db
- cursor.close();
- db.close();
- return counter;
- }
- /**
- * Populates the Future activities section of the home screen.
- * If there are future activities, makes the section visible.
- * Adds two rows with the two next activities.
- * Tapping them takes to the post, tapping anywhere else, to the blog section.
- *
- * @return The number of entries shown
- */
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- @SuppressWarnings("ResultOfMethodCallIgnored")
- private int setUpFutureActivities(View view) {
- int counter = 0;
- String lang = GM.getLang();
- LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_future_content);
- LinearLayout entry;
- //An inflater
- LayoutInflater factory = LayoutInflater.from(getActivity());
- //Get data from the database of the future activities
- SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
- Cursor cursor = db.rawQuery("SELECT id, date, city, title_" + lang + " AS title, text_" + lang + " AS text, price FROM activity WHERE date >= date('now') ORDER BY date LIMIT 2;", null);
- //If there are future activities...
- if (cursor.getCount() > 0) {
- //Show section
- LinearLayout llActivitiesFuture = (LinearLayout) view.findViewById(R.id.ll_home_section_activities_future);
- llActivitiesFuture.setVisibility(View.VISIBLE);
- //Set click listener
- llActivitiesFuture.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_ACTIVITIES, false);
- }
- });
- //Print rows
- while (cursor.moveToNext()) {
- //Create a new row
- entry = (LinearLayout) factory.inflate(R.layout.row_home_activities, null);
- //Set margins
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- layoutParams.setMargins(10, 10, 10, 25);
- entry.setLayoutParams(layoutParams);
- //Set title
- TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_home_activity_title);
- tvTitle.setText(cursor.getString(3));
- //Set city
- TextView tvCity = (TextView) entry.findViewById(R.id.tv_row_home_activity_city);
- tvCity.setText(cursor.getString(2));
- //Set price
- TextView tvPrice = (TextView) entry.findViewById(R.id.tv_row_home_activity_price);
- tvPrice.setText(String.format(getString(R.string.price), cursor.getInt(5)));
- //Set text
- String text = Html.fromHtml(cursor.getString(4)).toString();
- if (text.length() > 100) {
- text = text.substring(0, 100) + "...";
- }
- TextView tvText = (TextView) entry.findViewById(R.id.tv_row_home_activity_text);
- tvText.setText(text);
- //Set date
- TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_home_activity_date);
- tvDate.setText(GM.formatDate(cursor.getString(1) + " 00:00:00", lang, false));
- //Set hidden id
- TextView tvId = (TextView) entry.findViewById(R.id.tv_row_home_activity_hidden);
- tvId.setText(cursor.getString(0));
- //Get image
- ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_home_activity);
- Cursor cursorImage = db.rawQuery("SELECT image FROM activity_image WHERE activity = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
- if (cursorImage.getCount() > 0) {
- cursorImage.moveToFirst();
- String image = cursorImage.getString(0);
- //Check if image exists
- File f;
- f = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
- if (f.exists()) {
- //If the image exists, set it.
- Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image);
- iv.setImageBitmap(myBitmap);
- } else {
- //If not, create directories and download asynchronously
- File fpath;
- fpath = new File(this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/");
- fpath.mkdirs();
- new DownloadImage(GM.SERVER + "/img/actividades/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/actividades/miniature/" + image, iv, GM.IMG_MINIATURE).execute();
- }
- }
- cursorImage.close();
- //Set onCLickListener
- entry.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment fragment = new ActivityFutureLayout();
- Bundle bundle = new Bundle();
- //Pass post id
- int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_home_activity_hidden)).getText().toString());
- bundle.putInt("activity", id);
- fragment.setArguments(bundle);
- FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- ft.replace(R.id.activity_main_content_fragment, fragment);
- ft.addToBackStack("activity_" + id);
- ft.commit();
- }
- });
- //Add to the list
- llList.addView(entry);
- counter++;
- }
- }
- //Close db
- cursor.close();
- db.close();
- return counter;
- }
- /**
- * Populates the Blog section of the home screen.
- * Adds two rows with the two latest posts. Tapping them takes to the post, tapping anywhere else, to the blog section.
- *
- * @return The number of entries shown
- */
- @SuppressLint("InflateParams") //Throws unknown error when done properly.
- @SuppressWarnings("ResultOfMethodCallIgnored")
- private int setUpBlog(View view) {
- int counter = 0;
- String lang = GM.getLang();
- LinearLayout llBlog = (LinearLayout) view.findViewById(R.id.ll_home_section_blog);
- llBlog.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ((MainActivity) getActivity()).loadSection(GM.SECTION_BLOG, false);
- }
- });
- //Assign elements
- LinearLayout llList = (LinearLayout) view.findViewById(R.id.ll_home_section_blog_content);
- LinearLayout entry;
- //An inflater
- LayoutInflater factory = LayoutInflater.from(getActivity());
- SQLiteDatabase db = SQLiteDatabase.openDatabase(getActivity().getDatabasePath(GM.DB_NAME).getAbsolutePath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READONLY);
- final Cursor cursor;
- //Get data from the database
- cursor = db.rawQuery("SELECT id, title_" + lang + " AS title, text_" + lang + " AS text, dtime FROM post ORDER BY dtime DESC LIMIT 2;", null);
- //Loop
- while (cursor.moveToNext()) {
- //Create a new row
- entry = (LinearLayout) factory.inflate(R.layout.row_home_blog, null);
- //Set margins
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- layoutParams.setMargins(10, 10, 10, 25);
- entry.setLayoutParams(layoutParams);
- //Set title
- TextView tvTitle = (TextView) entry.findViewById(R.id.tv_row_home_blog_title);
- tvTitle.setText(cursor.getString(1));
- //Set text
- String text = Html.fromHtml(cursor.getString(2)).toString();
- if (text.length() > 100) {
- text = text.substring(0, 100) + "...";
- }
- TextView tvText = (TextView) entry.findViewById(R.id.tv_row_home_blog_text);
- tvText.setText(text);
- //Set date
- TextView tvDate = (TextView) entry.findViewById(R.id.tv_row_home_blog_date);
- tvDate.setText(GM.formatDate(cursor.getString(3), lang, false));
- //Set hidden id
- TextView tvId = (TextView) entry.findViewById(R.id.tv_row_home_blog_hidden);
- tvId.setText(cursor.getString(0));
- //Get image
- ImageView iv = (ImageView) entry.findViewById(R.id.iv_row_home_blog_image);
- Cursor cursorImage = db.rawQuery("SELECT image FROM post_image WHERE post = " + cursor.getString(0) + " ORDER BY idx LIMIT 1;", null);
- if (cursorImage.getCount() > 0) {
- cursorImage.moveToFirst();
- String image = cursorImage.getString(0);
- //Check if image exists
- File f;
- f = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image);
- if (f.exists()) {
- //If the image exists, set it.
- Bitmap myBitmap = BitmapFactory.decodeFile(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image);
- iv.setImageBitmap(myBitmap);
- } else {
- //If not, create directories and download asynchronously
- File fpath;
- fpath = new File(this.getActivity().getFilesDir().toString() + "/img/blog/miniature/");
- fpath.mkdirs();
- new DownloadImage(GM.SERVER + "/img/blog/miniature/" + image, this.getActivity().getFilesDir().toString() + "/img/blog/miniature/" + image, iv, GM.IMG_MINIATURE).execute();
- }
- }
- cursorImage.close();
- //Set onCLickListener
- entry.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Fragment fragment = new PostLayout();
- Bundle bundle = new Bundle();
- //Pass post id
- int id = Integer.parseInt(((TextView) v.findViewById(R.id.tv_row_home_blog_hidden)).getText().toString());
- bundle.putInt("post", id);
- fragment.setArguments(bundle);
- FragmentManager fm = HomeLayout.this.getActivity().getFragmentManager();
- FragmentTransaction ft = fm.beginTransaction();
- ft.replace(R.id.activity_main_content_fragment, fragment);
- ft.addToBackStack("post_" + id);
- ft.commit();
- }
- });
- //Add to the list
- llList.addView(entry);
- counter++;
- }
- //Close database
- cursor.close();
- db.close();
- return counter;
- }
- /**
- * Called when the user location changes.
- * Recalculates the list of around events and calls updateLocation() to
- * update the distance in the location section.
- *
- * @param location The new location
- *
- * @see android.location.LocationListener#onLocationChanged(android.location.Location)
- */
- @Override
- public void onLocationChanged(Location location) {
- setUpLocation(location, view);
- }
- /**
- * Called when the location provider changes it's state.
- * Recalculates the list of events in the around section.
- *
- * @param provider The name of the provider
- * @param status Status code of the provider
- * @param extras Extras passed
- *
- * @see android.location.LocationListener#onStatusChanged(java.lang.String, int, android.os.Bundle)
- */
- @Override
- public void onStatusChanged(String provider, int status, Bundle extras) {
- //populateAround();
- }
- /**
- * Called when a location provider is enabled.
- * Recalculates the list of events.
- *
- * @param provider The name of the provider
- *
- * @see android.location.LocationListener#onProviderEnabled(java.lang.String)
- */
- @Override
- public void onProviderEnabled(String provider) {
- //populateAround();
- }
- /**
- * Called when a location provider is disabled.
- * Recalculates the list of events.
- *
- * @param provider The name of the provider
- *
- * @see android.location.LocationListener#onProviderDisabled(java.lang.String)
- */
- @Override
- public void onProviderDisabled(String provider) {
- //populateAround();
- }
- /**
- * Called when the fragment is paused.
- * Stops the location manager
- * @see android.app.Fragment#onPause()
- */
- @Override
- public void onPause() {
- //TODO: Only do this if location is required
- 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)) {
- locationManager.removeUpdates(this);
- }
- super.onPause();
- }
- /**
- * Called when the fragment is destroyed.
- * Stops the location manager
- * @see android.app.Fragment#onDestroy()
- */
- @Override
- public void onDestroy() {
- //TODO: Only do this if location is required
- try {
- 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)) {
- locationManager.removeUpdates(this);
- }
- }
- catch (Exception ex){
- Log.e("Location manager", "Unable to stop location manager on destroy: " + ex.toString());
- }
- super.onDestroy();
- }
-
- /**
- * 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(){
- //TODO: Only do this if location is required
- 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)) {
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
- }
- super.onResume();
- }
- }
|