Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add power consumption detection #1086

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

22 changes: 22 additions & 0 deletions .idea/DoKit.iml

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

26 changes: 26 additions & 0 deletions .idea/gradle.xml

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

12 changes: 12 additions & 0 deletions .idea/misc.xml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

1 change: 1 addition & 0 deletions Android/dokit-powerdetection/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
47 changes: 47 additions & 0 deletions Android/dokit-powerdetection/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
plugins {
id 'com.android.application'
}
//apply plugin: 'aop'

android {
compileSdk 32
defaultConfig {
applicationId "com.example.androidpowercomsumption"
minSdk 26
targetSdk 30
versionCode 38
versionName "2.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

buildFeatures {
viewBinding true
}
}

dependencies {

implementation 'androidx.lifecycle:lifecycle-process:2.5.1'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment:2.5.2'
implementation 'androidx.navigation:navigation-ui:2.5.2'
implementation 'androidx.test:monitor:1.5.0'
testImplementation 'junit:junit:'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
}
1 change: 1 addition & 0 deletions Android/dokit-powerdetection/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ARTIFACT_ID=dokitx-powerdetection
21 changes: 21 additions & 0 deletions Android/dokit-powerdetection/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.androidpowercomsumption;

import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.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 <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.androidpowercomsumption", appContext.getPackageName());
}
}
37 changes: 37 additions & 0 deletions Android/dokit-powerdetection/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidpowercomsumption">

<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name="com.example.androidpowercomsumption.utils.AppStateApplication"
android:requestLegacyExternalStorage="true"
android:theme="@style/Theme.AndroidPowerComsumption">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.AndroidPowerComsumption.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

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

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.androidpowercomsumption;

import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LifecycleObserver;

public class MainActivity extends AppCompatActivity implements LifecycleObserver {

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

}


@Override
protected void onStop() {
super.onStop();
}


private boolean mIsExit;
/**
* 双击返回键退出
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mIsExit) {
this.finish();

} else {
Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
mIsExit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mIsExit = false;
}
}, 2000);
}
return true;
}
return super.onKeyDown(keyCode, event);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.example.androidpowercomsumption.controller;

import android.util.Log;
import com.example.androidpowercomsumption.utils.monitor.LogFileWriter;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AppStateController {

private static final String TAG = "AppStateController";
public long startTime; // 监控开始时间

public long endTime; // 监控结束时间

public long foregroundTime; // 前台运行时长

public long backgroundTime; // 后台运行时长

public boolean status; // true 前台 false 后台
public long curStatusStartTime; // 当前状态的开始时间

public long curStatusEndTime; // 当前状态的结束时间

public double foregroundRatio; //前台运行时间占比

public double backgroundRatio; // 后台运行时间占比


public void start() {

this.startTime = System.currentTimeMillis();
}

public void finish() {
this.endTime = System.currentTimeMillis();
this.foregroundRatio = foregroundTime * 1.0 / (this.foregroundTime + this.backgroundTime);
this.backgroundRatio = backgroundTime * 1.0 / (this.foregroundTime + this.backgroundTime);
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd/HH:mm:ss");
Date startDate = new Date(this.startTime);
Date endDate = new Date(this.endTime);
Log.d(TAG, "前台运行时间:" + this.foregroundTime);
LogFileWriter.write("前台运行时间:" + this.foregroundTime + " ms");

Log.d(TAG, "后台运行时间:" + this.backgroundTime);
LogFileWriter.write("后台运行时间:" + this.backgroundTime + " ms");

Log.d(TAG, "总运行时间:" + format.format(startDate) + "~" + format.format(endDate));

Log.d(TAG, "前台运行时间占比:" + this.foregroundRatio);
LogFileWriter.write("前台运行时间占比:" + this.foregroundRatio);

Log.d(TAG, "后台运行时间占比:" + this.backgroundRatio);
LogFileWriter.write("后台运行时间占比:" + this.backgroundRatio);


}
}
Loading