Skip to content

Commit

Permalink
improve notification system & ui, change logo
Browse files Browse the repository at this point in the history
  • Loading branch information
tanjim17 committed Jan 27, 2021
1 parent 19466e9 commit 048fe69
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 66 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "tj.beataddiction"
minSdkVersion 24
targetSdkVersion 29
versionCode 3
versionName "1.2"
versionCode 4
versionName "1.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -17,6 +17,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

repositories {
Expand Down
Binary file modified app/release/app-release.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":3,"versionName":"1.2","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":4,"versionName":"1.3","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
46 changes: 41 additions & 5 deletions app/src/main/java/tj/beataddiction/AppInfoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.pm.PackageManager;
import android.icu.util.Calendar;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
Expand All @@ -21,6 +22,7 @@

public class AppInfoActivity extends AppCompatActivity {
private static final String TAG = "AppInfoActivity";
private static final int LAUNCH_SETTINGS_ACTIVITY = 1;
private DatabaseHelper dbHelper;
private String packageName;
private String appName;
Expand All @@ -44,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_app_info);

if(!Utils.isUsageAccessAllowed(this)) {
Toast.makeText(getApplicationContext(), "You need to give usage access!", Toast.LENGTH_LONG).show();
openUsageDialog();
}

packageName = getIntent().getStringExtra("packageName");
Expand Down Expand Up @@ -77,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDialog();
openTrackingDialog();
}
});
}
Expand Down Expand Up @@ -178,11 +180,33 @@ private void editTrackingInfo() {
if(editedTimeAllowed > getTimeSpent()) {
dbHelper.resetIsUsageExceeded(packageName);
}
Toast.makeText(getApplicationContext(),"Changes Saved",Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"Changes Saved!",Toast.LENGTH_LONG).show();
finish();
}

private void openDialog() {
private void openUsageDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Usage Access Needed :(")
.setMessage("You need to give usage access to this app to see usage data of your apps. " +
"Click \"Go To Settings\" and then give the access :)")
.setPositiveButton("Go To Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent usageAccessIntent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivityForResult(usageAccessIntent, LAUNCH_SETTINGS_ACTIVITY);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(false);
builder.show();
}

private void openTrackingDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Do you want to stop tracking this app?")
.setNegativeButton("No", new DialogInterface.OnClickListener() {
Expand All @@ -198,10 +222,22 @@ public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
});
})
.setCancelable(false);
builder.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == LAUNCH_SETTINGS_ACTIVITY) {
if(Utils.isUsageAccessAllowed(this)) {
Alarms.scheduleNotification(this);
}
}
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/tj/beataddiction/BackgroundService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tj.beataddiction;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
Expand All @@ -13,6 +14,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class BackgroundService extends JobIntentService {
private DatabaseHelper dbHelper;
Expand All @@ -36,6 +38,10 @@ protected void onHandleWork(@NonNull Intent intent) {
long endTime = beginTime + Utils.DAY_IN_MILLIS;
HashMap<String, Integer> appUsageMap = Utils.getTimeSpent(this, null, beginTime, endTime);

String currentRunningPackageName = null;
List<String> list = appUsageMap.keySet().stream().filter(s -> s.startsWith("current")).collect(Collectors.toList());
if(list.size() > 0) {currentRunningPackageName = list.get(0).replaceFirst("current", "");}

for(int i = 0; i < trackedAppInfos.size(); i++) {
TrackedAppInfo trackedAppInfo = trackedAppInfos.get(i);
String packageName = trackedAppInfo.getPackageName();
Expand All @@ -46,7 +52,8 @@ protected void onHandleWork(@NonNull Intent intent) {
int allowedTime = trackedAppInfo.getTimeAllowed();
int isUsageExceeded = trackedAppInfo.getIsUsageExceeded();

if(usageTime > allowedTime && isUsageExceeded == 0) {
if((usageTime > allowedTime && isUsageExceeded == 0) ||
(isUsageExceeded == 1 && packageName.equals(currentRunningPackageName))) {
try {
dbHelper.setIsUsageExceeded(packageName);
String appName = (String) getPackageManager()
Expand Down
4 changes: 0 additions & 4 deletions app/src/main/java/tj/beataddiction/ChartActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);

if(!Utils.isUsageAccessAllowed(this)) {
Toast.makeText(getApplicationContext(), "You need to give usage access!", Toast.LENGTH_LONG).show();
}

packageName = getIntent().getStringExtra("packageName");
appName = getIntent().getStringExtra("appName");

Expand Down
27 changes: 4 additions & 23 deletions app/src/main/java/tj/beataddiction/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ private void startBackgroundService() {
if(Utils.isUsageAccessAllowed(this)) {
Alarms.scheduleNotification(getApplicationContext());
}
else {
Intent usageAccessIntent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivityForResult(usageAccessIntent, LAUNCH_SETTINGS_ACTIVITY);
Toast.makeText(getApplicationContext(), "You need to give usage access!", Toast.LENGTH_LONG).show();
}
}

private void openDialog() {
Expand All @@ -117,37 +112,23 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("IMPORTANT!")
.setMessage("You might not get usage notifications after reboot if your device is " +
"from certain manufacturers(eg. Xiaomi). To solve this issue, you can either " +
.setMessage("You might not get usage notifications after reboot. To solve this issue, you can either " +
"give auto start permission for this app manually from settings or launch " +
"this app at least once after reboot.")
"this app at least once after reboot. That's all :)")
.setView(checkBoxView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
})
.setCancelable(false);

if(sharedPreferences.getBoolean("openDialog", true)) {
builder.show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == LAUNCH_SETTINGS_ACTIVITY) {
if(Utils.isUsageAccessAllowed(this)) {
Alarms.scheduleNotification(this);
}
else {
Toast.makeText(getApplicationContext(), "You need to give usage access!", Toast.LENGTH_LONG).show();
}
}
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/tj/beataddiction/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ static HashMap<String, Integer> getTimeSpent(Context context, String packageName
if(allEvents.size() != 0) {
UsageEvents.Event lastEvent = allEvents.get(allEvents.size() - 1);
if (lastEvent.getEventType() == UsageEvents.Event.ACTIVITY_RESUMED) {
String currentRunningPackageName = lastEvent.getPackageName();
int diff = (int) System.currentTimeMillis() - (int) lastEvent.getTimeStamp();
diff /= 1000;
Integer prev = appUsageMap.get(lastEvent.getPackageName());
Integer prev = appUsageMap.get(currentRunningPackageName);
if (prev == null) prev = 0;
appUsageMap.put(lastEvent.getPackageName(), prev + diff);
appUsageMap.put(currentRunningPackageName, prev + diff);
appUsageMap.put("current" + currentRunningPackageName, -1); //for notification purpose
}
}

Expand Down
Binary file modified app/src/main/res/drawable/app_logo.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 modified app/src/main/res/drawable/splash_screen_logo.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 048fe69

Please sign in to comment.