Skip to content

Commit

Permalink
Added code to open announcement links in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
therealsujitk committed Jan 10, 2021
1 parent f3df722 commit 022d2ed
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package tk.therealsuji.vtopchennai;

import android.animation.AnimatorInflater;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
Expand Down Expand Up @@ -50,6 +56,22 @@ public void setAnnouncements(View view) {
((HorizontalScrollView) findViewById(R.id.categoriesContainer)).smoothScrollTo((int) location - halfWidth, 0);
}

public void openLink(String link) {
if (link.equals("NA")) {
Dialog noLink = new Dialog(this);
noLink.setContentView(R.layout.dialog_nolink);
noLink.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
noLink.show();
} else if (link.startsWith("http")) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
startActivity(browserIntent);
} else {
String downloadLink = "http://vtopcc.vit.ac.in/vtop/" + link + "?&x=";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadLink));
startActivity(browserIntent);
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -66,7 +88,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void run() {
SQLiteDatabase myDatabase = context.openOrCreateDatabase("vtop", Context.MODE_PRIVATE, null);

myDatabase.execSQL("CREATE TABLE IF NOT EXISTS spotlight (id INT(3) PRIMARY KEY, category VARCHAR, announcement VARCHAR)");
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS spotlight (id INT(3) PRIMARY KEY, category VARCHAR, announcement VARCHAR, link VARCHAR)");
Cursor c = myDatabase.rawQuery("SELECT DISTINCT category FROM spotlight", null);

int categoryIndex = c.getColumnIndex("category");
Expand All @@ -84,7 +106,7 @@ public void run() {
LinearLayout.LayoutParams.MATCH_PARENT
);
announcementsView.setLayoutParams(announcementsViewParams);
announcementsView.setPadding(0, (int) (65 * pixelDensity), 0, (int) (15 * pixelDensity));
announcementsView.setPadding(0, (int) (65 * pixelDensity), 0, 0);
announcementsView.setOrientation(LinearLayout.VERTICAL);

announcementViews.add(announcementsView); //Storing the view
Expand Down Expand Up @@ -133,9 +155,10 @@ public void run() {
}
});

Cursor s = myDatabase.rawQuery("SELECT announcement FROM spotlight WHERE category = '" + categoryTitle + "'", null);
Cursor s = myDatabase.rawQuery("SELECT announcement, link FROM spotlight WHERE category = '" + categoryTitle + "'", null);

int announcementIndex = s.getColumnIndex("announcement");
int linkIndex = s.getColumnIndex("link");
s.moveToFirst();

for (int j = 0; j < s.getCount(); ++j, s.moveToNext()) {
Expand All @@ -149,12 +172,30 @@ public void run() {
);
blockParams.setMarginStart((int) (20 * pixelDensity));
blockParams.setMarginEnd((int) (20 * pixelDensity));
blockParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (5 * pixelDensity));
if (j == s.getCount() - 1) {
blockParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (20 * pixelDensity));
} else {
blockParams.setMargins(0, (int) (5 * pixelDensity), 0, (int) (5 * pixelDensity));
}
block.setLayoutParams(blockParams);
block.setBackground(ContextCompat.getDrawable(context, R.drawable.plain_card));
block.setClickable(true);
block.isFocusable();
block.setBackground(ContextCompat.getDrawable(context, R.drawable.button_card));
block.setStateListAnimator(AnimatorInflater.loadStateListAnimator(context, R.animator.item_elevation));
block.setGravity(Gravity.CENTER_VERTICAL);
block.setOrientation(LinearLayout.VERTICAL);

/*
Setting the onClickListener
*/
final String link = s.getString(linkIndex);
block.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openLink(link);
}
});

/*
The announcement TextView
*/
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/res/layout/dialog_nolink.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/container"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:background="@drawable/dialog_background"
android:orientation="vertical"
android:paddingStart="20dp"
android:paddingTop="20dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_max="320dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_link"
android:textColor="@color/colorText"
android:textSize="20sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:text="@string/no_link_text"
android:textColor="@color/colorText"
android:textSize="16sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
<string name="no_messages">No Messages</string>
<string name="no_announcements">No Announcements</string>

<string name="no_link">Link Unavailable</string>
<string name="no_link_text">Sorry, there is no link attached to this announcement.</string>

<string name="new_message">Looks like you do have some new messages, however <a href="https://therealsuji.tk">@therealsujitk</a> was a bit too lazy to add this feature, why don\'t you contact him and give him a piece of your mind.</string>


Expand Down

0 comments on commit 022d2ed

Please sign in to comment.