From 3e4e9fe5cb986d70ba4864720d4ec1220edd7ce8 Mon Sep 17 00:00:00 2001 From: Ishaan Date: Tue, 6 Aug 2019 22:15:22 +0530 Subject: [PATCH] Initial commit --- .gitignore | 13 ++ .idea/dictionaries/ishaan.xml | 3 + .idea/encodings.xml | 4 + .idea/gradle.xml | 15 ++ .idea/misc.xml | 52 ++++++ .idea/runConfigurations.xml | 12 ++ app/.gitignore | 1 + app/build.gradle | 30 +++ app/proguard-rules.pro | 21 +++ .../usagestats/ExampleInstrumentedTest.java | 27 +++ app/src/main/AndroidManifest.xml | 25 +++ .../xymenapps/usagestats/MainActivity.java | 146 +++++++++++++++ .../drawable-v24/ic_launcher_foreground.xml | 34 ++++ .../res/drawable/ic_launcher_background.xml | 170 +++++++++++++++++ app/src/main/res/layout/activity_main.xml | 98 ++++++++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + app/src/main/res/mipmap-hdpi/ic_launcher.png | Bin 0 -> 2963 bytes .../res/mipmap-hdpi/ic_launcher_round.png | Bin 0 -> 4905 bytes app/src/main/res/mipmap-mdpi/ic_launcher.png | Bin 0 -> 2060 bytes .../res/mipmap-mdpi/ic_launcher_round.png | Bin 0 -> 2783 bytes app/src/main/res/mipmap-xhdpi/ic_launcher.png | Bin 0 -> 4490 bytes .../res/mipmap-xhdpi/ic_launcher_round.png | Bin 0 -> 6895 bytes .../main/res/mipmap-xxhdpi/ic_launcher.png | Bin 0 -> 6387 bytes .../res/mipmap-xxhdpi/ic_launcher_round.png | Bin 0 -> 10413 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.png | Bin 0 -> 9128 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.png | Bin 0 -> 15132 bytes app/src/main/res/values/colors.xml | 6 + app/src/main/res/values/strings.xml | 3 + app/src/main/res/values/styles.xml | 11 ++ .../xymenapps/usagestats/ExampleUnitTest.java | 17 ++ build.gradle | 27 +++ gradle.properties | 20 ++ gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 54329 bytes gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 172 ++++++++++++++++++ gradlew.bat | 84 +++++++++ settings.gradle | 1 + 38 files changed, 1008 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/dictionaries/ishaan.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/gradle.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/runConfigurations.xml create mode 100644 app/.gitignore create mode 100644 app/build.gradle create mode 100644 app/proguard-rules.pro create mode 100644 app/src/androidTest/java/com/xymenapps/usagestats/ExampleInstrumentedTest.java create mode 100644 app/src/main/AndroidManifest.xml create mode 100644 app/src/main/java/com/xymenapps/usagestats/MainActivity.java create mode 100644 app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 app/src/main/res/layout/activity_main.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-hdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-mdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher.png create mode 100644 app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png create mode 100644 app/src/main/res/values/colors.xml create mode 100644 app/src/main/res/values/strings.xml create mode 100644 app/src/main/res/values/styles.xml create mode 100644 app/src/test/java/com/xymenapps/usagestats/ExampleUnitTest.java create mode 100644 build.gradle create mode 100644 gradle.properties create mode 100644 gradle/wrapper/gradle-wrapper.jar create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b75303 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild diff --git a/.idea/dictionaries/ishaan.xml b/.idea/dictionaries/ishaan.xml new file mode 100644 index 0000000..bfb99ae --- /dev/null +++ b/.idea/dictionaries/ishaan.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..2996d53 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28bd97d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,52 @@ + + + + + + + + + + + + + + + + + + Android + + + CorrectnessLintAndroid + + + Java + + + LintAndroid + + + Probable bugsJava + + + RELAX NG + + + + + Android + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml new file mode 100644 index 0000000..7f68460 --- /dev/null +++ b/.idea/runConfigurations.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..796b96d --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..a0df789 --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,30 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 28 + defaultConfig { + applicationId "com.xymenapps.usagestats" + minSdkVersion 22 + targetSdkVersion 28 + versionCode 1 + versionName "1.0" + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar']) + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'androidx.constraintlayout:constraintlayout:1.1.3' + testImplementation 'junit:junit:4.12' + androidTestImplementation 'androidx.test:runner:1.2.0' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + + api 'com.google.guava:guava:28.0-android' +} diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..f1b4245 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# 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 *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile diff --git a/app/src/androidTest/java/com/xymenapps/usagestats/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/xymenapps/usagestats/ExampleInstrumentedTest.java new file mode 100644 index 0000000..160db2c --- /dev/null +++ b/app/src/androidTest/java/com/xymenapps/usagestats/ExampleInstrumentedTest.java @@ -0,0 +1,27 @@ +package com.xymenapps.usagestats; + +import android.content.Context; + +import androidx.test.InstrumentationRegistry; +import androidx.test.runner.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getTargetContext(); + + assertEquals("com.xymenapps.usagestats", appContext.getPackageName()); + } +} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..1375724 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/xymenapps/usagestats/MainActivity.java b/app/src/main/java/com/xymenapps/usagestats/MainActivity.java new file mode 100644 index 0000000..18a76de --- /dev/null +++ b/app/src/main/java/com/xymenapps/usagestats/MainActivity.java @@ -0,0 +1,146 @@ +package com.xymenapps.usagestats; + +import androidx.appcompat.app.AppCompatActivity; + +import android.app.AppOpsManager; +import android.app.usage.UsageEvents; +import android.app.usage.UsageStatsManager; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import android.os.Bundle; +import android.os.Process; +import android.provider.Settings; +import android.text.method.ScrollingMovementMethod; +import android.util.Log; +import android.view.View; +import android.widget.TextView; +import android.widget.Toast; + +import com.google.common.base.Joiner; + +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static android.app.AppOpsManager.MODE_ALLOWED; +import static android.app.AppOpsManager.OPSTR_GET_USAGE_STATS; + +public class MainActivity extends AppCompatActivity { + + private static final String TAG = MainActivity.class.getSimpleName(); + String currentApp; + UsageStatsManager usm = null; + Map appsOpened; + long todayInMs; + long hourToMs = 60 * 60 * 1000; + TextView result; + + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + + AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); + int mode = appOps.checkOpNoThrow(OPSTR_GET_USAGE_STATS, Process.myUid(), getPackageName()); + boolean usage_permission = mode == MODE_ALLOWED; + appsOpened = new HashMap<>(); + result = findViewById(R.id.result_textview); + result.setMovementMethod(new ScrollingMovementMethod()); + result.setTextSize(15); + + Calendar today = Calendar.getInstance(); + long elapsed = today.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 + + today.get(Calendar.MINUTE) * 60 * 1000 + + today.get(Calendar.MILLISECOND) * 1000; + todayInMs = today.getTimeInMillis() - elapsed; + Log.d(TAG, "onCreate: Today in ms : " + todayInMs); + Log.d(TAG, "onCreate: Today: " + new Date(todayInMs)); + + + if (usage_permission) { + Log.d(TAG, "onCreate: Permission granted"); + usm = (UsageStatsManager) this.getSystemService(Context.USAGE_STATS_SERVICE); +// findAppsOpened(System.currentTimeMillis() - 3600 * 1000, System.currentTimeMillis()); + } else { + startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)); + Log.d(TAG, "onCreate: Permission denied"); + Toast.makeText(this, "Grant access and relaunch activity", Toast.LENGTH_LONG).show(); + } + } + + private void findAppsOpened(long startTime, long endTime) { + Log.d(TAG, "findAppsOpened() called with: startTime = [" + startTime + "], endTime = [" + endTime + "]"); +// List appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime); +// if (appList != null && appList.size() > 0) { +// SortedMap mySortedMap = new TreeMap(); +// for (UsageStats usageStats : appList) { +// if (usageStats.getPackageName().equals(BuildConfig.APPLICATION_ID)) { +// Log.d(TAG, "First TimeStamp: " + new Date(usageStats.getFirstTimeStamp())); +// Log.d(TAG, "Last TimeStamp: " + new Date(usageStats.getLastTimeStamp())); +// Log.d(TAG, "Last Time Used: " + new Date(usageStats.getLastTimeUsed())); +// Log.d(TAG, "Totol foreground time: " + usageStats.getTotalTimeInForeground() / 1000 + " seconds"); +// Log.d(TAG, "describeContents: " + usageStats.describeContents()); +// } +// mySortedMap.put(usageStats.getLastTimeUsed(), usageStats); +// } +// if (mySortedMap != null && !mySortedMap.isEmpty()) { +// currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); +// } +// Log.e("CURRENT APP", "Current App in foreground is: " + currentApp); +// } + Log.d(TAG, "findAppsOpened: FROM: " + new Date(startTime) + " TO: " + new Date(endTime)); + try { + UsageEvents usageEvents = usm.queryEvents(startTime, endTime); + UsageEvents.Event event = new UsageEvents.Event(); + while (usageEvents.hasNextEvent()) { + usageEvents.getNextEvent(event); + if (event.getEventType() == 1) { + String packageName = event.getPackageName(); + long timestamp = event.getTimeStamp(); + Calendar eventTime = Calendar.getInstance(); + eventTime.setTimeInMillis(timestamp); +// Log.d(TAG, packageName + " : " + new Date(timestamp)); + appsOpened.put(packageName, eventTime.get(Calendar.HOUR) + ":" + eventTime.get(Calendar.MINUTE) + " " + (eventTime.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM")); + } + } + displayResult(); + } catch (NullPointerException e) { + Toast.makeText(this, "Error!!", Toast.LENGTH_SHORT).show(); + e.printStackTrace(); + } + } + + + private void displayResult() { + Log.d(TAG, "displayResult() called"); + result.setText(Joiner.on("\n").withKeyValueSeparator(" : ").join(appsOpened)); + } + + public void find(View view) { + int id = view.getId(); + switch (id) { + case R.id.button2: + findAppsOpened(todayInMs, todayInMs + 4 * hourToMs); + break; + case R.id.button3: + findAppsOpened(todayInMs + 4 * hourToMs, todayInMs + 8 * hourToMs); + break; + case R.id.button4: + findAppsOpened(todayInMs + 8 * hourToMs, todayInMs + 12 * hourToMs); + break; + case R.id.button5: + findAppsOpened(todayInMs + 12 * hourToMs, todayInMs + 16 * hourToMs); + break; + case R.id.button6: + findAppsOpened(todayInMs + 16 * hourToMs, todayInMs + 20 * hourToMs); + break; + case R.id.button7: + findAppsOpened(todayInMs + 20 * hourToMs, todayInMs + 24 * hourToMs); + break; + } + } +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1f6bb29 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..0d025f9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..4c1dab4 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,98 @@ + + + + +