rewrite layout and catch url errors

This commit is contained in:
akaessens 2020-03-25 23:48:37 +01:00
parent 065cc04667
commit 3c6c51e781
9 changed files with 287 additions and 360 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
TODO.txt
*.iml
.gradle
/local.properties

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -1,5 +1,7 @@
package com.akdev.nofbeventscraper;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
@ -10,20 +12,18 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class FbScraper extends AsyncTask<Void, Void, Void> {
private String url;
private View popupView;
private MainActivity main;
public FbEvent event;
public FbScraper(View popupView, String url) {
public FbScraper(MainActivity main, String url) {
this.url = url;
this.popupView = popupView;
this.main = main;
}
@Override
@ -34,10 +34,6 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
try {
document = Jsoup.connect(url).get();
Log.d("url", url);
//String event_name = document.title();
String json = document.select("#u_0_j").first().data();
try {
@ -49,11 +45,12 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
String event_description = reader.getString("description");
String location = reader.getJSONObject("location").getString("name");
String image_url = reader.getString("image");
this.event = new FbEvent(event_name, event_start, event_end, event_description, location, image_url);
} catch (JSONException e) {
e.printStackTrace();
}
@ -77,16 +74,7 @@ public class FbScraper extends AsyncTask<Void, Void, Void> {
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
TextInputEditText field_name = (TextInputEditText) this.popupView.findViewById(R.id.field_event_name);
TextInputEditText field_start = (TextInputEditText) this.popupView.findViewById(R.id.field_event_start);
TextInputEditText field_end = (TextInputEditText) this.popupView.findViewById(R.id.field_event_end);
TextInputEditText field_location = (TextInputEditText) this.popupView.findViewById(R.id.field_event_location);
field_name.setText(event.name);
field_start.setText(event.start_date);
field_end.setText(event.end_date);
field_location.setText(event.location);
main.update(event);
}
}

View File

@ -1,40 +1,37 @@
package com.akdev.nofbeventscraper;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.textfield.TextInputEditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.provider.CalendarContract;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private Button paste_button;
private Button cancel_button;
private Button ok_button;
private TextInputEditText field_uri_input;
private TextInputEditText field_event_name;
private TextInputEditText field_event_start;
private TextInputEditText field_event_end;
private TextInputEditText field_event_location;
private TextInputEditText field_event_description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -42,17 +39,110 @@ public class MainActivity extends AppCompatActivity {
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
cancel_button = (Button) findViewById(R.id.cancel_button);
ok_button = (Button) findViewById(R.id.ok_button);
paste_button = (Button) findViewById(R.id.paste_button);
fab.setOnClickListener(new View.OnClickListener() {
field_uri_input = (TextInputEditText) findViewById(R.id.field_uri_input);
field_event_name = (TextInputEditText) findViewById(R.id.field_event_name);
field_event_start = (TextInputEditText) findViewById(R.id.field_event_start);
field_event_end = (TextInputEditText) findViewById(R.id.field_event_end);
field_event_location = (TextInputEditText) findViewById(R.id.field_event_location);
field_event_description = (TextInputEditText) findViewById(R.id.field_event_description);
final MainActivity mainactivity = this;
paste_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onButtonShowPopupWindowClick(view);
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard != null) {
String str = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
try {
new URL(str).toURI();
if (str.substring(0,32).equals("https://www.facebook.com/events/")) {
str = str.replace("www.", "m.");
}
else if (str.substring(0,30).equals("https://m.facebook.com/events/")) {
}
else {
throw new Exception();
}
field_uri_input.setText(str);
FbScraper scraper = new FbScraper(mainactivity, field_uri_input.getText().toString());
scraper.execute();
}
catch (Exception e) {
clear();
}
}
}
});
cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clear();
}
});
ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// time
String start_str = field_event_start.getText().insert(22, ":").toString();
String end_str = field_event_end.getText().insert(22, ":").toString();
LocalDateTime start = LocalDateTime.parse(start_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
LocalDateTime end = LocalDateTime.parse(end_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
ZoneId zoneId = ZoneId.systemDefault();
long start_epoch = start.atZone(zoneId).toEpochSecond() * 1000;
long end_epoch = end.atZone(zoneId).toEpochSecond() * 1000;
String name = field_event_name.getText().toString();
String location = field_event_location.getText().toString();
String description = field_event_description.getText().toString();
String uri = field_uri_input.getText().toString();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, field_event_name.getText().toString());
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start_epoch);
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end_epoch);
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, field_event_location.getText().toString());
intent.putExtra(CalendarContract.Events.DESCRIPTION, uri + "\n" + description);
startActivity(intent);
}
});
}
@Override
public void clear() {
field_uri_input.setText("");
field_event_name.setText("");
field_event_start.setText("");
field_event_end.setText("");
field_event_location.setText("");
field_event_description.setText("");
}
public void update(FbEvent event) {
field_event_name.setText(event.name);
field_event_start.setText(event.start_date);
field_event_end.setText(event.end_date);
field_event_location.setText(event.location);
field_event_description.setText(event.description);
}
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
@ -72,101 +162,5 @@ public class MainActivity extends AppCompatActivity {
}
return super.onOptionsItemSelected(item);
}
}*/
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.popup_window, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window token
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
Button cancel_button = (Button)popupView.findViewById(R.id.cancel_button);
cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
popupWindow.dismiss();
}
});
Button ok_button = (Button)popupView.findViewById(R.id.ok_button);
ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextInputEditText uri_input = (TextInputEditText) popupView.findViewById(R.id.text_input);
TextInputEditText field_name = (TextInputEditText) popupView.findViewById(R.id.field_event_name);
TextInputEditText field_start = (TextInputEditText) popupView.findViewById(R.id.field_event_start);
TextInputEditText field_end = (TextInputEditText) popupView.findViewById(R.id.field_event_end);
TextInputEditText field_location = (TextInputEditText) popupView.findViewById(R.id.field_event_location);
// time
String start_str = field_start.getText().insert(22, ":").toString();
String end_str = field_end.getText().insert(22, ":").toString();
LocalDateTime start = LocalDateTime.parse(start_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
LocalDateTime end = LocalDateTime.parse(end_str, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
ZoneId zoneId = ZoneId.systemDefault();
long start_epoch = start.atZone(zoneId).toEpochSecond()*1000;
long end_epoch = end.atZone(zoneId).toEpochSecond()*1000;
//Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, field_name.getText().toString());
intent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, start_epoch);
intent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end_epoch);
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, field_location.getText().toString());
intent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI, uri_input.getText().toString());
startActivity(intent);
popupWindow.dismiss();
}
});
Button paste_button = (Button)popupView.findViewById(R.id.paste_button);
final TextInputEditText text_input = (TextInputEditText) popupView.findViewById(R.id.text_input);
paste_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (clipboard != null) {
String str = clipboard.getPrimaryClip().getItemAt(0).getText().toString();
str = "https://m.facebook.com/events/2402761143327832/";
text_input.setText(str);
}
FbScraper scraper = new FbScraper(popupView, text_input.getText().toString());
FbEvent event;
scraper.execute();
}
});
/* dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});*/
}
}

View File

@ -20,16 +20,10 @@
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<include
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:tint="#ffffff"
app:backgroundTint="?attr/colorPrimary"
app:srcCompat="@android:drawable/ic_input_add" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -3,20 +3,169 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="match_parent">
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/textView"
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:text="@string/hello_message"
android:layout_marginStart="16dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="16dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="@+id/paste_button"
style="@style/Widget.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@android:string/paste" />
</LinearLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout4">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_uri_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:clickable="true"
android:cursorVisible="true"
android:hint="@string/add_link_hint"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<ScrollView
android:id="@+id/scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toTopOf="@+id/bottomButtonLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:cursorVisible="true"
android:hint="Event name"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:cursorVisible="true"
android:hint="Event start"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:cursorVisible="true"
android:hint="Event end"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:cursorVisible="true"
android:hint="Event location"
android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:cursorVisible="true"
android:hint="Event description"
android:singleLine="false" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/bottomButtonLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/cancel" />
<Button
android:id="@+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -1,180 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/popup_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/text_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:hint="@string/add_link_hint"
android:overScrollMode="always"
android:scrollHorizontally="true"
app:layout_constraintBottom_toBottomOf="@+id/paste_button"
app:layout_constraintEnd_toStartOf="@+id/paste_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/paste_button"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@android:string/paste"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_input"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/label_name"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Name" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="300dp"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Event name" />
</com.google.android.material.textfield.TextInputLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Event start time" />
</com.google.android.material.textfield.TextInputLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Event end time" />
</com.google.android.material.textfield.TextInputLayout>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/label_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/field_event_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Event location" />
</com.google.android.material.textfield.TextInputLayout>
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/cancel" />
<Button
android:id="@+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok" />
</LinearLayout>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</RelativeLayout>

View File

@ -1,20 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
tools:src="@tools:sample/avatars"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="5dp"
tools:text="@tools:sample/lorem" />
</LinearLayout>

View File

@ -2,5 +2,5 @@
<string name="app_name">NoFb Event Scraper</string>
<string name="action_settings">Settings</string>
<string name="hello_message">This app scrapes facebook event links and adds the event to your calendar.</string>
<string name="add_link_hint">Enter facebook link to the event.</string>
<string name="add_link_hint">Paste facebook link to the event.</string>
</resources>