Skip to content

Commit

Permalink
V2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
etet2007 committed Jun 2, 2018
1 parent 829f9b2 commit 9586df8
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 149 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.chenyee.stephenlau.floatingball"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 260
versionName "2.6"
versionCode 270
versionName "2.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
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"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":260,"versionName":"2.6","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":270,"versionName":"2.7","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
</intent-filter>
</service>

<activity android:name=".util.ScreenCaptureImageActivity"
<activity android:name=".activity.ScreenCaptureImageActivity"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:theme="@style/Theme.Transparent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void onCreate() {
}

public static App getApplication() {

return mInstance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.Image;
Expand All @@ -20,70 +20,58 @@
* Created by omerjerk on 17/2/16.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public class Screenshotter implements ImageReader.OnImageAvailableListener {
private static final String TAG = "Screenshotter";
private static Screenshotter mInstance;
public class Screenshot implements ImageReader.OnImageAvailableListener {
private static final String TAG = "Screenshot";
private static Screenshot mInstance;

private VirtualDisplay virtualDisplay;
private int width;
private int height;
private int mScreenDensity = 0;

private ScreenshotCallback cb;
private ImageReader mImageReader;
private MediaProjection mMediaProjection;

/**
* Get the single instance of the Screenshotter class.
* Get the single instance of the Screenshot class.
* @return the instance
*/
public static Screenshotter getInstance() {
public static Screenshot getInstance() {
if (mInstance == null) {
mInstance = new Screenshotter();
mInstance = new Screenshot();
}

return mInstance;
}

private Screenshotter() {}
public Screenshot() {}

/**
* Takes the screenshot of whatever currently is on the default display.
* @param resultCode The result code returned by the request for accessing MediaProjection permission
* @param data The intent returned by the same request
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public Screenshotter takeScreenshot(Context context, int resultCode, Intent data, final ScreenshotCallback cb) {
// Context context1 = context;
public Screenshot takeScreenshot(Context context, int resultCode, Intent data, ScreenshotCallback cb) {
this.cb = cb;

// int resultCode1 = resultCode;
// Intent data1 = data;

//get width and height
WindowManager windowManager = (WindowManager) App.getApplication().getSystemService(Context.WINDOW_SERVICE);
// Point size = new Point();
// windowManager.getDefaultDisplay().getSize(size);
// width = size.x;//1080
// height = size.y;//2034
//get dpi
DisplayMetrics metrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getRealMetrics(metrics);
mScreenDensity = metrics.densityDpi;
int screenDensity = metrics.densityDpi;
width = metrics.widthPixels;
height = metrics.heightPixels;

//ImageReader
mImageReader = ImageReader.newInstance(width, height, 0x1,1);
mImageReader.setOnImageAvailableListener(Screenshotter.this, null);
//create ImageReader
mImageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888,1);
mImageReader.setOnImageAvailableListener(Screenshot.this, null);

MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) context
.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
mMediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data);
try {
//VirtualDisplay
virtualDisplay = mMediaProjection.createVirtualDisplay("Screenshotter",
width, height, mScreenDensity,
//create virtualDisplay
virtualDisplay = mMediaProjection.createVirtualDisplay("screenshot",
width, height, screenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mImageReader.getSurface(), null, null);
} catch (Exception e) {
Expand All @@ -99,7 +87,7 @@ public Screenshotter takeScreenshot(Context context, int resultCode, Intent data
* @param height height of the request bitmap
* @return the singleton instance
*/
public Screenshotter setSize(int width, int height) {
public Screenshot setSize(int width, int height) {
this.width = width;
this.height = height;
return this;
Expand All @@ -108,7 +96,7 @@ public Screenshotter setSize(int width, int height) {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
Image image;
try {
image = reader.acquireLatestImage();
}catch (UnsupportedOperationException e){
Expand All @@ -134,6 +122,8 @@ public void onImageAvailable(ImageReader reader) {
Bitmap bitmap = Bitmap.createBitmap(width+rowPadding/pixelStride, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(buffer);
bitmap = Bitmap.createBitmap(bitmap, 0, 0,width, height);
image.close();

//回调
cb.onScreenshot(bitmap);

Expand All @@ -145,7 +135,7 @@ public void onImageAvailable(ImageReader reader) {
mMediaProjection.stop();
mMediaProjection = null;
}
image.close();
mImageReader = null;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import android.graphics.Bitmap;

/**
* Created by fg607 on 16-4-16.
*/
public interface ScreenshotCallback {

void onScreenshot(Bitmap bitmap);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.chenyee.stephenlau.floatingball.util;
package com.chenyee.stephenlau.floatingball.activity;
/*
* Copyright (C) 2014 The Android Open Source Project
*
Expand All @@ -17,59 +17,35 @@

import android.Manifest;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Environment;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.RequiresApi;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.chenyee.stephenlau.floatingball.R;
import com.chenyee.stephenlau.floatingball.ScreenshotCallback;
import com.chenyee.stephenlau.floatingball.Screenshotter;
import com.chenyee.stephenlau.floatingball.Screenshot;
import com.chenyee.stephenlau.floatingball.floatBall.FloatingBallService;
import com.chenyee.stephenlau.floatingball.util.BitmapUtil;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.media.Image;
import android.media.ImageReader;
import android.os.Handler;
import android.os.Looper;
import android.view.Display;
import android.view.OrientationEventListener;
import android.view.View.OnClickListener;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
import static com.chenyee.stephenlau.floatingball.util.StaticStringUtil.EXTRA_TYPE;

public class ScreenCaptureImageActivity extends Activity {
public static final String TAG = ScreenCaptureImageActivity.class.getSimpleName();

Expand All @@ -81,10 +57,7 @@ public class ScreenCaptureImageActivity extends Activity {
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

private boolean mIsExist =false;
private Handler mHandler = new Handler();

private Messenger mMessenger = null;
// private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -95,43 +68,42 @@ protected void onCreate(Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
takeScreenshot();
} else {
Toast.makeText(this, "当前系统不支持快捷截屏!", Toast.LENGTH_SHORT).show();

mHandler.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 500);
Toast.makeText(getApplicationContext(), getString(R.string.no_support_screenshot), Toast.LENGTH_SHORT).show();
finish();
}


}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void takeScreenshot() {
MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
//隐藏悬浮球
// sendMsg(Config.HIDE_BALL, "hide", true);
setBallIsHide(true);

MediaProjectionManager mediaProjectionManager = (MediaProjectionManager)
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
if (mediaProjectionManager != null) {
startActivityForResult(
mediaProjectionManager.createScreenCaptureIntent(),
REQUEST_MEDIA_PROJECTION);
}
}

/**
* 处理回调的信息。
* @param requestCode
* @param resultCode
* @param data
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_MEDIA_PROJECTION) {
if (resultCode == RESULT_OK) {
Screenshotter.getInstance()
.takeScreenshot(this, resultCode, data, new ScreenshotCallback() {
//返回的数据在data中
Screenshot.getInstance()//其实不懂为什么要使用单例子?
.takeScreenshot(getApplicationContext(), resultCode, data, new ScreenshotCallback() {
@Override
public void onScreenshot(final Bitmap bitmap) {
Log.d(TAG, "onScreenshot: bitmap "+bitmap);

//文件处理 另开线程
new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -140,33 +112,28 @@ public void run() {
String strDate = dateFormat.format(date);
String dir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Screenshots/";

BitmapUtil.copyImageToExterl(bitmap,dir,strDate+".png");
String fileName = strDate + ".jpg";

BitmapUtil.copyImageToExternal(bitmap, dir, fileName);

bitmap.recycle();
}
}).start();
Toast.makeText(ScreenCaptureImageActivity.this, "截图成功!", Toast.LENGTH_SHORT).show();

// if(!mIsExist){
// sendMsg(Config.HIDE_BALL, "hide", false);
// Toast.makeText(ScreenCaptureImageActivity.this, "截图失败!", Toast.LENGTH_SHORT).show();
// mIsExist = true;
// }

// unbindFloatService();

Toast.makeText(getApplicationContext(), getString(R.string.screenshot_succeed), Toast.LENGTH_SHORT).show();
setBallIsHide(false);
finish();
}
});
}else {
Toast.makeText(ScreenCaptureImageActivity.this, "截图失败!", Toast.LENGTH_SHORT).show();
Toast.makeText(ScreenCaptureImageActivity.this, getString(R.string.screenshot_fail), Toast.LENGTH_SHORT).show();
}

} else {
Toast.makeText(this, "You denied the permission.", Toast.LENGTH_SHORT).show();
Toast.makeText(this, getString(R.string.screenshot_fail), Toast.LENGTH_SHORT).show();
}
}


public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Expand All @@ -185,11 +152,12 @@ public void onBackPressed() {
moveTaskToBack(false);
}

public void sendMsg(int what,String name,boolean action) {
Intent intent = new Intent();
intent.putExtra("what",what);
intent.putExtra(name, action);
// intent.setClass(this, FloatService.class);
private void setBallIsHide(boolean isHide) {
Bundle bundle = new Bundle();
bundle.putInt(EXTRA_TYPE, FloatingBallService.TYPE_HIDE);
bundle.putBoolean("isHide",isHide);
Intent intent = new Intent(this, FloatingBallService.class)
.putExtras(bundle);
startService(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

Expand Down Expand Up @@ -235,5 +236,9 @@ public void moveBallViewDown() {
public void clear() {
if(mFloatingBallView !=null) mFloatingBallView.recycleBitmap();
}

public void setVisibility(boolean isHide) {
if(mFloatingBallView !=null) mFloatingBallView.setVisibility(isHide?View.INVISIBLE:View.VISIBLE);
}
}

Loading

0 comments on commit 9586df8

Please sign in to comment.