Skip to content
This repository has been archived by the owner on May 20, 2020. It is now read-only.

Commit

Permalink
Notification on new release and/or commit to github
Browse files Browse the repository at this point in the history
  • Loading branch information
claha committed Jan 16, 2015
1 parent 6734e39 commit aa35bf7
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 11 deletions.
94 changes: 94 additions & 0 deletions app/src/main/java/com/claha/showtimeremote/GitHubHTTP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.claha.showtimeremote;

import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

public class GitHubHTTP {

private static final String URL_GITHUB = "https://github.com/claha/showtimeremote";

public void run() {
new Download().execute();
}

public interface OnCommitsCountedListener {
public void onCounted(int count);
}

private OnCommitsCountedListener onCommitsCountedListener;

public void setOnCommitsCountedListener(OnCommitsCountedListener onCommitsCountedListener) {
this.onCommitsCountedListener = onCommitsCountedListener;
}

public interface OnReleasesCountedListener {
public void onCounted(int count);
}

private OnReleasesCountedListener onReleasesCountedListener;

public void setOnReleasesCountedListener(OnReleasesCountedListener onReleasesCountedListener) {
this.onReleasesCountedListener = onReleasesCountedListener;
}

private class Download extends AsyncTask {

private List<String> html;

@Override
protected Object doInBackground(Object[] params) {
try {
URL url = new URL(URL_GITHUB);
URLConnection connection = url.openConnection();
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
html = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
html.add(line);
}
reader.close();
} catch (Exception e) {
Log.d("GitHubHTTP", "ERROR");
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(Object o) {
boolean foundCommits = false;
boolean foundReleases = false;
int commits = -1;
int releases = -1;
int i = 0;
while (!(foundCommits && foundReleases)) {
if (!foundCommits) {
if (html.get(i).contains("/commits") && html.get(i).contains("data-pjax")) {
Log.d("GitHubHTTP", html.get(i));
commits = Integer.parseInt(html.get(i + 3).replace(" ", ""));
foundCommits = true;
}
} else if (!foundReleases) {
if (html.get(i).contains("/releases")) {
Log.d("GitHubHTTP", html.get(i));
releases = Integer.parseInt(html.get(i + 3).replace(" ", ""));
foundReleases = true;
}
}
i++;
}
onCommitsCountedListener.onCounted(commits);
onReleasesCountedListener.onCounted(releases);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -82,12 +81,9 @@ public void onCreate(final Bundle savedInstanceState) {

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(TAG, "onPreferenceChange");

// Profile add
if (preference == profilesAdd) {
Log.d(TAG, "profiles_add");

String name = profilesAdd.getEditText().getText().toString();
profiles.add(new Profile(name, networkIPAddress.getText(), networkPort.getText()));

Expand Down Expand Up @@ -161,24 +157,20 @@ public boolean onPreferenceClick(Preference preference) {
}

private void loadProfiles() {
Log.d(TAG, "saveProfiles");
Set<String> profilesToLoadTemp = sharedPreferences.getStringSet("profiles", new HashSet<String>());
List<String> profilesToLoad = new ArrayList<>(profilesToLoadTemp);
Collections.sort(profilesToLoad);
Log.d(TAG, "number of profiles: " + profilesToLoad.size());
profiles = new ArrayList<>();
for (String profile : profilesToLoad) {
profiles.add(new Profile(profile));
}
}

private void saveProfiles() {
Log.d(TAG, "saveProfiles");
Set<String> profilesToSave = new HashSet<>();
for (Profile profile : profiles) {
profilesToSave.add(profile.toString());
}
Log.d(TAG, "number of profiles: " + profilesToSave.size());
sharedPreferences.edit().putStringSet("profiles", profilesToSave).apply();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.claha.showtimeremote;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

public class ShowtimeNotification extends NotificationCompat.Builder {

private final Context context;
private final NotificationManager notificationManager;
private final String message;
private String url = null;

public ShowtimeNotification(Context context, String message) {
super(context);
this.context = context;
this.message = message;

notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

setSmallIcon(R.drawable.ic_launcher);
setContentTitle("Showtime Remote");
setContentText(message);
setAutoCancel(true);
}

public void setUrl(String url) {
this.url = url;
}

public void show() {
if (url != null) {
Intent resultIntent = new Intent(Intent.ACTION_VIEW);
resultIntent.setData(Uri.parse(url));

PendingIntent pending = PendingIntent.getActivity(context, 0, resultIntent, 0);
setContentIntent(pending);
}
notificationManager.notify(message.hashCode(), build());
}
}
44 changes: 43 additions & 1 deletion app/src/main/java/com/claha/showtimeremote/ShowtimeRemote.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.preference.PreferenceManager;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

Expand All @@ -14,12 +15,17 @@

public class ShowtimeRemote extends NavigationDrawerActivity {

private static final String TAG = "ShowtimeRemote";
private final ShowtimeHTTP showtimeHTTP = new ShowtimeHTTP();
private SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager.setDefaultValues(this, R.xml.fragment_settings, false);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

setupNotifications();
}

@Override
Expand Down Expand Up @@ -105,7 +111,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

showtimeHTTP.setIpAddress(sharedPreferences.getString("ipAddress", null));
showtimeHTTP.setPort(sharedPreferences.getString("port", null));

Expand All @@ -126,4 +132,40 @@ public boolean onQueryTextChange(String newText) {
return showOptionsMenu;
}

private void setupNotifications() {
final boolean notifyCommit = sharedPreferences.getBoolean("notify_commit", false);
final boolean notifyRelease = sharedPreferences.getBoolean("notify_release", false);

GitHubHTTP gitHubHTTP = new GitHubHTTP();

gitHubHTTP.setOnCommitsCountedListener(new GitHubHTTP.OnCommitsCountedListener() {
@Override
public void onCounted(int count) {
int prevCount = sharedPreferences.getInt("notify_commit_count", 0);
sharedPreferences.edit().putInt("notify_commit_count", count).apply();

if (prevCount != 0 && notifyCommit && count > prevCount) {
Log.d(TAG, "Notify commit");
new ShowtimeNotification(getApplicationContext(), "There is a new commit to GitHub").show();
}
}
});

gitHubHTTP.setOnReleasesCountedListener(new GitHubHTTP.OnReleasesCountedListener() {
@Override
public void onCounted(int count) {
int prevCount = sharedPreferences.getInt("notiy_notify_release_count", 0);
sharedPreferences.edit().putInt("notiy_notify_release_count", count).apply();

if (prevCount != 0 && notifyRelease && count > prevCount) {
Log.d(TAG, "Notify release");
ShowtimeNotification notification = new ShowtimeNotification(getApplicationContext(), "There is a new release on GitHub");
notification.setUrl("http://www.github.com/claha/showtimeremote/releases");
notification.show();
}
}
});

gitHubHTTP.run();
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<resources>

<color name="colorPrimary">#FF236B8E</color>
<color name="colorPrimaryDark">#FF377FA2</color>
<color name="colorPrimaryDark">#FF0F577A</color>
<color name="colorAccent">@color/colorPrimary</color>

<color name="textColorPrimary">#FFFFFFFF</color>
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
<string name="settings_port_summary">Port of Showtime</string>
<string name="settings_port_defaultValue">42000</string>

<string name="settings_notify_commit_key">notfiy_commit</string>
<string name="settings_notify_commit_summary">Notify on new commit</string>
<string name="settings_notify_commit_title">Commit</string>

<string name="settings_notify_release_key">notfiy_release</string>
<string name="settings_notify_release_summary">Notify on new release</string>
<string name="settings_notify_release_title">Release</string>

<string name="settings_misc_start_page_key">misc_start_page</string>
<string name="settings_misc_start_page_summary">Choose startup page</string>
<string name="settings_misc_start_page_title">Start Page</string>
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/res/xml/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@

</PreferenceCategory>

<PreferenceCategory android:title="GitHub Notifications">

<CheckBoxPreference
android:defaultValue="false"
android:key="@string/settings_notify_commit_key"
android:summary="@string/settings_notify_commit_summary"
android:title="@string/settings_notify_commit_title" />

<CheckBoxPreference
android:defaultValue="true"
android:key="@string/settings_notify_release_key"
android:summary="@string/settings_notify_release_summary"
android:title="@string/settings_notify_release_title" />

</PreferenceCategory>

<PreferenceCategory android:title="Miscellaneous">

<ListPreference
Expand All @@ -67,5 +83,4 @@

</PreferenceCategory>


</PreferenceScreen>

0 comments on commit aa35bf7

Please sign in to comment.