Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Davide Ricci committed Feb 27, 2017
0 parents commit 710283d
Show file tree
Hide file tree
Showing 34 changed files with 782 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "it.trenitalia.feedbackmanutentivi"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.volley:volley:1.0.0'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in F:\Android\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.majin.officedoor;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.majin.officedoor", appContext.getPackageName());
}
}
35 changes: 35 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.majin.officedoor">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:icon="@drawable/preview"
android:label="Office Widget"
android:name="com.majin.officedoor.SimpleWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/simple_widget_info" />
</receiver>
</application>

<uses-permission android:name="android.permission.INTERNET" />

</manifest>
106 changes: 106 additions & 0 deletions app/src/main/java/com/majin/officedoor/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.majin.officedoor;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

public class MainActivity extends Activity {

private TextView resultText;
private ProgressBar progressBar;
private ImageButton mainButton;

private boolean enableButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

enableButton = true;

resultText = (TextView) findViewById(R.id.main_text_result);

progressBar = (ProgressBar) findViewById(R.id.main_progress_bar);
progressBar.setIndeterminate(true);

mainButton = (ImageButton) findViewById(R.id.main_open_button);

stopRequest();
mainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startRequest();
if (enableButton)
callSimpleServer();
}
});
}

private void callSimpleServer() {

RequestQueue queue = Volley.newRequestQueue(this);
String url = Registry.URL;
enableButton = false;

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.

stopRequest();
if (response != null && response.contains("open")) {
mainButton.setImageResource(R.drawable.open_green);
resultText.setText("Door opened");
} else {
mainButton.setImageResource(R.drawable.open_red);
resultText.setText("Door closed");
}

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mainButton.setImageResource(R.drawable.open);

}
}, 4000);

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
resultText.setText("That didn't work!");
stopRequest();
mainButton.setImageResource(R.drawable.open_red);
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}

private void startRequest() {
resultText.setVisibility(View.VISIBLE);

mainButton.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.VISIBLE);
}

private void stopRequest() {
mainButton.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.INVISIBLE);
enableButton = true;
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/com/majin/officedoor/Registry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.majin.officedoor;

/**
* Created by Majin on 26/02/2017.
*/

public class Registry {

private static Registry instance;

public static String URL = "http://10.0.2.76?on?json";

private boolean buttonEnabled = true;

public static void initInstance()
{
if (instance == null)
{
// Create the instance
instance = new Registry();
}
}

public static Registry getInstance()
{
// Return the instance
initInstance();
return instance;
}

private Registry()
{
// Constructor hidden because this is a singleton
}

public boolean isButtonEnabled() {
return buttonEnabled;
}

public void setButtonEnabled(boolean buttonEnabled) {
this.buttonEnabled = buttonEnabled;
}
}
108 changes: 108 additions & 0 deletions app/src/main/java/com/majin/officedoor/SimpleWidgetProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.majin.officedoor;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.RemoteViews;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

/**
* Created by Majin on 26/02/2017.
*/

public class SimpleWidgetProvider extends AppWidgetProvider {

private RemoteViews remoteViews;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];

remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget);

remoteViews.setViewVisibility(R.id.main_progress_bar_widget, View.INVISIBLE);
if (Registry.getInstance().isButtonEnabled()) {
callService(context);
}
Intent intent = new Intent(context, SimpleWidgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}

private void callService(final Context context) {
RequestQueue queue = Volley.newRequestQueue(context);
String url = Registry.URL;

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
ComponentName thisWidget = new ComponentName(context, SimpleWidgetProvider.class);
remoteViews.setViewVisibility(R.id.main_progress_bar_widget, View.VISIBLE);

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
callIntent(context, response);

Registry.getInstance().setButtonEnabled(false);

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
callIntent(context, "!");
Registry.getInstance().setButtonEnabled(true);
}
},4000);

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callIntent(context, "KO");
}
});
queue.add(stringRequest);
}

private void callIntent(Context context, String value) {

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
ComponentName thisWidget = new ComponentName(context, SimpleWidgetProvider.class);

remoteViews.setViewVisibility(R.id.main_progress_bar_widget, View.INVISIBLE);

if (value.equalsIgnoreCase("!"))
remoteViews.setImageViewResource(R.id.actionButton, R.drawable.open);
else if (value.equalsIgnoreCase("OK") || value.contains("open"))
remoteViews.setImageViewResource(R.id.actionButton, R.drawable.open_green);
else if (value.equalsIgnoreCase("KO") || value.contains("close"))
remoteViews.setImageViewResource(R.id.actionButton, R.drawable.open_red);

appWidgetManager.updateAppWidget(thisWidget, remoteViews);

}

}
Binary file added app/src/main/res/drawable/aa.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/open.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/open_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/open_red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/res/drawable/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 710283d

Please sign in to comment.