Skip to content

Commit

Permalink
the app will now show if the background service is on or off
Browse files Browse the repository at this point in the history
  • Loading branch information
DevLuuk committed Aug 28, 2018
1 parent f06172a commit 65616c4
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public void onDestroy() {
super.onDestroy();
}



@Override
public IBinder onBind(Intent intent) {
return null;
Expand Down
79 changes: 70 additions & 9 deletions app/src/main/java/nl/devluuk/sleepywifi/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,101 @@
package nl.devluuk.sleepywifi;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getSimpleName();
public int state;
public String status;
public String state;
public Boolean status;
public Drawable playIcon;
public Drawable grayIcon;

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


final TextView stateText = findViewById(R.id.OnOffText);
Button startButton = (Button) findViewById(R.id.OnOffButton);

startButton.setOnClickListener(new View.OnClickListener() {
final ImageView imageButton = findViewById(R.id.powerButton);
getStatus();
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stateText.setText(state);
playOrPauseService(imageButton);
}
});

startService(new Intent(this, BackgroundService.class));

}

public void playOrPauseService(ImageView image) {
final TextView stateText = findViewById(R.id.OnOffText);
if (getStatus()) {
stopService(new Intent(this, BackgroundService.class));

grayIcon = getResources().getDrawable(R.drawable.ic_launcher_round_gray, null);
makeGrayIcon(grayIcon);
image.setImageDrawable(grayIcon);

stateText.setText("Current state is: " + setState());
} else {
startService(new Intent(this, BackgroundService.class));

playIcon = getResources().getDrawable(R.drawable.ic_launcher_round, null);

image.setImageDrawable(playIcon);

stateText.setText("Current state is: " + setState());
}
}

private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}

private Drawable makeGrayIcon(Drawable icon){
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);

ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
icon.setColorFilter(filter);
return icon;
}

private boolean getStatus() {
status = isMyServiceRunning(BackgroundService.class);
return status;
}

private String setState() {
if (status) {
state = "OFF";
} else {
state = "ON";
}
return state;
}

}
Binary file added app/src/main/res/drawable/ic_launcher_round.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/power.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- drawable/power.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M16.56,5.44L15.11,6.89C16.84,7.94 18,9.83 18,12A6,6 0 0,1 12,18A6,6 0 0,1 6,12C6,9.83 7.16,7.94 8.88,6.88L7.44,5.44C5.36,6.88 4,9.28 4,12A8,8 0 0,0 12,20A8,8 0 0,0 20,12C20,9.28 18.64,6.88 16.56,5.44M13,3H11V13H13" />
</vector>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/power_standby.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- drawable/power-standby.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:width="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path android:fillColor="#000" android:pathData="M13,3H11V13H13V3M17.83,5.17L16.41,6.59C18.05,7.91 19,9.9 19,12A7,7 0 0,1 12,19C8.14,19 5,15.88 5,12C5,9.91 5.95,7.91 7.58,6.58L6.17,5.17C2.38,8.39 1.92,14.07 5.14,17.86C8.36,21.64 14.04,22.1 17.83,18.88C19.85,17.17 21,14.65 21,12C21,9.37 19.84,6.87 17.83,5.17Z" />
</vector>
21 changes: 11 additions & 10 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/OnOffButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="On/Off"
/>

<TextView
android:id="@+id/OnOffText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="178dp"
android:text="TextView" />
android:layout_marginTop="138dp"
android:text=""
android:textColor="@color/colorPrimary"
android:textSize="@dimen/tab_label"/>

<ImageView
android:id="@+id/powerButton"
android:layout_width="128dp"
android:layout_height="147dp"
android:layout_centerInParent="true"
android:src="@drawable/ic_launcher_round" />
</RelativeLayout>
9 changes: 6 additions & 3 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorPrimary">#125688</color>
<color name="colorPrimaryDark">#125688</color>
<color name="textColorPrimary">#FFFFFF</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="colorAccent">#c8e8ff</color>
</resources>
11 changes: 11 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="tab_max_width">264dp</dimen>
<dimen name="tab_padding_bottom">16dp</dimen>
<dimen name="tab_label">16dp</dimen>
<dimen name="nav_header_vertical_spacing">8dp</dimen>
<dimen name="nav_header_height">176dp</dimen>
<dimen name="fab_margin">16dp</dimen>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<resources>
<string name="app_name">SleepyWifi</string>
<string name="title_activity_settings">Settings</string>
<string name="on">On</string>
<string name="off">Off</string>
</resources>

0 comments on commit 65616c4

Please sign in to comment.