Skip to content

Commit

Permalink
#1 Capture screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
Jo Grimstad committed Sep 22, 2016
1 parent 4784b4b commit 1178e80
Show file tree
Hide file tree
Showing 26 changed files with 617 additions and 39 deletions.
45 changes: 6 additions & 39 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# Intellij
*.iml
.idea/workspace.xml

# Keystore files
*.jks
.gradle
/local.properties
/.idea
.DS_Store
/build
/captures
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
24 changes: 24 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "org.literacyapp.analytics"
minSdkVersion 21
targetSdkVersion 23
versionCode 1000000
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled false
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.literacyapp.analytics;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
22 changes: 22 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.literacyapp.analytics">

<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name">

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

<service
android:name=".service.ScreenshotJobService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
66 changes: 66 additions & 0 deletions app/src/main/java/org/literacyapp/analytics/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.literacyapp.analytics;

import android.app.Activity;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.literacyapp.analytics.service.ScreenshotJobService;
import org.literacyapp.analytics.util.RootHelper;

public class MainActivity extends Activity {

private Button mMainButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(getClass().getName(), "onCreate");
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mMainButton = (Button) findViewById(R.id.main_button);
}

@Override
protected void onStart() {
Log.i(getClass().getName(), "onStart");
super.onStart();

mMainButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(getClass().getName(), "mMainButton onClick");

// Request root access
boolean isRootPermissionGranted = RootHelper.runAsRoot(new String[] {
"echo \"Do I have root?\" >/system/sd/temporary.txt\n",
"exit\n"
});
Log.i(getClass().getName(), "isRootPermissionGranted: " + isRootPermissionGranted);
if (!isRootPermissionGranted) {
Toast.makeText(getApplicationContext(), "Root permission was not granted. Please restart application.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Root permission was granted. Starting background job...", Toast.LENGTH_LONG).show();

// Initiate background job for recording screenshots
ComponentName componentName = new ComponentName(getApplicationContext(), ScreenshotJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, componentName);
builder.setPeriodic(5 * 60 * 1000); // Every 5 minutes
JobInfo screenshotJobInfo = builder.build();

JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(screenshotJobInfo);
}

finish();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.literacyapp.analytics.service;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.util.Log;
import android.view.Display;

import org.literacyapp.analytics.util.DisplayHelper;

/**
* Service responsible for recording screenshots when the screen in switched on.
*/
public class ScreenshotJobService extends JobService {

@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.i(getClass().getName(), "onStartJob");

// Detect if screen is active
DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
for (Display display : displayManager.getDisplays()) {
if (display.getState() != Display.STATE_OFF) {
Log.i(getClass().getName(), "display.getState(): " + display.getState());
DisplayHelper.captureScreenshot(this);
}
}

boolean asynchronousProcessing = false;
return asynchronousProcessing;
}

@Override
public boolean onStopJob(JobParameters jobParameters) {
Log.i(getClass().getName(), "onStopJob");

boolean restartJob = false;
return restartJob;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.literacyapp.analytics.util;

import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;

import java.io.File;
import java.util.Calendar;

public class DisplayHelper {

public static void captureScreenshot(Context context) {
Log.i(DisplayHelper.class.getName(), "captureScreenshot");
String screenshotsPath = context.getFilesDir() + File.separator + "screenshots";
File screenshotsDir = new File(screenshotsPath);
if (!screenshotsDir.exists()) {
screenshotsDir.mkdir();
}

String dateFormatted = (String) DateFormat.format("yyyy-MM-dd_hh:mm:ss", Calendar.getInstance());
String fileName = "Screenshot_" + dateFormatted + ".png";
File screenshotFile = new File(screenshotsDir, fileName);
Log.i(DisplayHelper.class.getName(), "screenshotFile: " + screenshotFile);

boolean isScreencapSuccess = RootHelper.runAsRoot(new String[] {
"screencap " + screenshotFile.getAbsolutePath() + "\n"
});
Log.i(DisplayHelper.class.getName(), "isScreencapSuccess: " + isScreencapSuccess);
Log.i(DisplayHelper.class.getName(), "screenshotFile.exists(): " + screenshotFile.exists());

// Reduce image size
// TODO
}
}
56 changes: 56 additions & 0 deletions app/src/main/java/org/literacyapp/analytics/util/RootHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.literacyapp.analytics.util;

import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class RootHelper {

public static boolean runAsRoot(String[] commands) {
Log.i(RootHelper.class.getName(), "runAsRoot");

boolean isSuccess = false;

try {
Process process = Runtime.getRuntime().exec("su");

DataOutputStream dataOutputStream = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
Log.i(RootHelper.class.getName(), "command: " + command);
dataOutputStream.writeBytes(command + "\n");
}
dataOutputStream.writeBytes("exit\n");
dataOutputStream.flush();

process.waitFor();
int exitValue = process.exitValue();
Log.i(RootHelper.class.getName(), "exitValue: " + exitValue);
if (exitValue == 0) {
isSuccess = true;
}

InputStream inputStreamSuccess = process.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStreamSuccess));
String successMessage = bufferedReader.readLine();
Log.i(RootHelper.class.getName(), "successMessage: " + successMessage);

InputStream inputStreamError = process.getErrorStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStreamError));
String errorMessage = bufferedReader.readLine();
if (TextUtils.isEmpty(errorMessage)) {
Log.i(RootHelper.class.getName(), "errorMessage: " + errorMessage);
} else {
Log.e(RootHelper.class.getName(), "errorMessage: " + errorMessage);
}
} catch (IOException | InterruptedException e) {
Log.e(RootHelper.class.getName(), null, e);
}

return isSuccess;
}
}
22 changes: 22 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="org.literacyapp.analytics.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This application requires root access. Press 'Continue' and then 'Remember my choice'." />
<Button
android:id="@+id/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue" />
</LinearLayout>
Binary file added app/src/main/res/mipmap-hdpi/ic_launcher.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/mipmap-mdpi/ic_launcher.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/mipmap-xhdpi/ic_launcher.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/mipmap-xxhdpi/ic_launcher.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/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Analytics (LiteracyApp)</string>
</resources>
15 changes: 15 additions & 0 deletions app/src/test/java/org/literacyapp/analytics/ExampleUnitTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.literacyapp.analytics;

import org.junit.Test;

import static org.junit.Assert.*;

/**
* To work on unit tests, switch the Test Artifact in the Build Variants view.
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
28 changes: 28 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
mavenLocal()
jcenter()
mavenCentral()
maven {
url "http://archiva.educativo.eu:8081/repository/internal/"
}
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
Loading

0 comments on commit 1178e80

Please sign in to comment.