Commit effbc9af by Wang Xu

更改壁纸

parent 58b98f28
......@@ -41,10 +41,15 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
......@@ -136,12 +141,14 @@ import com.android.launcher3.widget.WidgetHostViewLoader;
import com.android.launcher3.widget.WidgetsContainerView;
import com.secspace.lib.common.event.RefreshWorkSpaceEvent;
import com.secspace.lib.common.utils.DpmHelper;
import com.secspace.lib.common.utils.FileUtil;
import com.secspace.mdmengine.api.manager.MdmFactoryManager;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.File;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
......@@ -161,6 +168,7 @@ public class Launcher extends BaseActivity
LauncherModel.Callbacks, View.OnTouchListener, LauncherProviderChangeListener,
AccessibilityManager.AccessibilityStateChangeListener {
public static final String TAG = "Launcher";
private static final String ACTION_CHANGE_WALLPAPER = "action_change_wallpaper";
static final boolean LOGD = false;
static final boolean DEBUG_WIDGETS = false;
......@@ -178,6 +186,8 @@ public class Launcher extends BaseActivity
private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
private static final int CHANGE_WALLPAPER = 14;
private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
/**
......@@ -206,6 +216,8 @@ public class Launcher extends BaseActivity
static final String APPS_VIEW_SHOWN = "launcher.apps_view_shown";
static final String WALLPAPER_NAME = "wallpaper";
/**
* The different states that Launcher can be in.
*/
......@@ -361,6 +373,8 @@ public class Launcher extends BaseActivity
private RotationPrefChangeHandler mRotationPrefChangeHandler;
private String mWallpaperPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (DEBUG_STRICT_MODE) {
......@@ -377,6 +391,7 @@ public class Launcher extends BaseActivity
.penaltyDeath()
.build());
}
mWallpaperPath = getFilesDir().getAbsolutePath() + File.separator + WALLPAPER_NAME;
if (LauncherAppState.PROFILE_STARTUP) {
Trace.beginSection("Launcher-onCreate");
}
......@@ -483,6 +498,22 @@ public class Launcher extends BaseActivity
mLauncherCallbacks.onCreate(savedInstanceState);
}
EventBus.getDefault().register(this);
if(mWallpaperPath!= null) {
Bitmap bitmap = BitmapFactory.decodeFile(mWallpaperPath);
View launcher = findViewById(R.id.launcher);
launcher.setBackground(new BitmapDrawable(getResources(), bitmap));
}
if(ACTION_CHANGE_WALLPAPER.equals(getIntent().getAction())) {
Uri selectedImage = getIntent().getParcelableExtra("uri");
String path = FileUtil.getPath(this, selectedImage);
FileUtil.copyFile(this, path,WALLPAPER_NAME);
Bitmap bitmap = BitmapFactory.decodeFile(mWallpaperPath);
View launcher = findViewById(R.id.launcher);
launcher.setBackground(new BitmapDrawable(getResources(), bitmap));
}
}
@Override
......@@ -1781,11 +1812,11 @@ public class Launcher extends BaseActivity
@Override
protected void onNewIntent(Intent intent) {
long startTime = 0;
if (DEBUG_RESUME_TIME) {
startTime = System.currentTimeMillis();
}
super.onNewIntent(intent);
boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
!= Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
......@@ -1853,6 +1884,16 @@ public class Launcher extends BaseActivity
mLauncherCallbacks.onHomeIntent();
}
}
if(ACTION_CHANGE_WALLPAPER.equals(intent.getAction())) {
Uri selectedImage = intent.getParcelableExtra("uri");
String path = FileUtil.getPath(this, selectedImage);
FileUtil.copyFile(this, path,WALLPAPER_NAME);
Bitmap bitmap = BitmapFactory.decodeFile(mWallpaperPath);
View launcher = findViewById(R.id.launcher);
launcher.setBackground(new BitmapDrawable(getResources(), bitmap));
}
PinItemDragListener.handleDragRequest(this, intent);
if (mLauncherCallbacks != null) {
......@@ -1881,7 +1922,6 @@ public class Launcher extends BaseActivity
});
}
}
if (DEBUG_RESUME_TIME) {
Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
}
......
......@@ -4,19 +4,28 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import com.android.launcher3.BuildConfig;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.mdm.MdmUtils;
import com.android.launcher3.settings.wifi.WifiWhiteListActivity;
import com.secspace.lib.common.utils.FileUtil;
public class SettingActivity extends AppCompatActivity implements View.OnClickListener {
private static final int REQUEST_PICK_WALLPAPER = 10;
private static final String ACTION_CHANGE_WALLPAPER = "action_change_wallpaper";
private Button mBtnVsrsion;
private Button mBtnChangeWallPaper;
private Button mBtnChangePwd;
......@@ -139,6 +148,28 @@ public class SettingActivity extends AppCompatActivity implements View.OnClickLi
case R.id.btn_wifi_whitelist:
startActivity(new Intent(this, WifiWhiteListActivity.class));
break;
case R.id.btn_change_wallpaper:
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
break;
}
}
@Override
protected void onActivityResult(
final int requestCode, final int resultCode, final Intent data) {
if (requestCode == REQUEST_PICK_WALLPAPER) {
if (resultCode == RESULT_OK) {
Log.e("WWWW","AAAAA");
Uri selectedImage = data.getData();
Intent intent = new Intent();
intent.setPackage(getPackageName());
intent.setAction(ACTION_CHANGE_WALLPAPER);
intent.setClass(this, Launcher.class);
intent.putExtra("uri",selectedImage);
startActivity(intent);
finish();
}
}
}
}
package com.secspace.lib.common.utils;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
......@@ -16,135 +24,29 @@ import java.text.DecimalFormat;
public class FileUtil {
private static final String TAG = "FileUtil";
/**
* 复制单个文件,如果目标文件存在,则不覆盖
*/
public static boolean copyFile(String srcFileName, String descFileName) {
return FileUtil.copyFileCover(srcFileName, descFileName, false);
}
/**
* 复制单个文件
*/
public static boolean copyFileCover(String srcFileName,
String descFileName, boolean coverlay) {
File srcFile = new File(srcFileName);
if (!srcFile.exists()) {
return false;
} else if (!srcFile.isFile()) {
return false;
}
File descFile = new File(descFileName);
if (descFile.exists()) {
if (coverlay) {
if (!deleteFile(descFileName)) {
return false;
}
} else {
return false;
}
} else {
if (!descFile.getParentFile().exists()) {
if (!descFile.getParentFile().mkdirs()) {
return false;
}
}
}
int readByte = 0;
InputStream ins = null;
OutputStream outs = null;
public static void copyFile(Context context,String oldPath, String newPath) {
try {
descFile.createNewFile();
ins = new FileInputStream(srcFile);
outs = new FileOutputStream(descFile);
byte[] buf = new byte[1024];
while ((readByte = ins.read(buf)) != -1) {
outs.write(buf, 0, readByte);
}
return true;
} catch (Exception e) {
return false;
} finally {
if (outs != null) {
try {
outs.close();
} catch (IOException oute) {
oute.printStackTrace();
}
}
if (ins != null) {
try {
ins.close();
} catch (IOException ine) {
ine.printStackTrace();
}
}
}
}
/**
* 复制整个目录的内容,如果目标目录存在,则不覆盖
*/
public static boolean copyDirectory(String srcDirName, String descDirName) {
return FileUtil.copyDirectoryCover(srcDirName, descDirName, false);
}
/**
* 复制整个目录的内容
*/
public static boolean copyDirectoryCover(String srcDirName,
String descDirName, boolean coverlay) {
File srcDir = new File(srcDirName);
if (!srcDir.exists()) {
return false;
} else if (!srcDir.isDirectory()) {
return false;
}
if (!descDirName.endsWith(File.separator)) {
descDirName = descDirName + File.separator;
}
File descDir = new File(descDirName);
if (descDir.exists()) {
if (coverlay) {
if (!deleteFile(descDirName)) {
return false;
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
FileOutputStream fos = context.openFileOutput(newPath, Context.MODE_PRIVATE);
if (oldfile.exists()) {
InputStream inStream = new FileInputStream(oldPath);
byte[] buffer = new byte[1024];
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fos.write(buffer, 0, byteread);
}
} else {
return false;
inStream.close();
}
} else {
if (!descDir.mkdirs()) {
return false;
}
}
boolean flag = true;
File[] files = srcDir.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
flag = FileUtil.copyFile(files[i].getAbsolutePath(),
descDirName + files[i].getName());
if (!flag) {
break;
}
}
if (files[i].isDirectory()) {
flag = FileUtil.copyDirectory(files[i].getAbsolutePath(),
descDirName + files[i].getName());
if (!flag) {
break;
}
}
}
if (!flag) {
return false;
catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* 删除文件,可以删除单个文件或文件夹
*/
......@@ -351,4 +253,119 @@ public class FileUtil {
return null;
}
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
}
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = MediaStore.Images.Media.DATA;
final String[] projection = {column};
try {
cursor = context.getContentResolver().query(uri, null, selection, selectionArgs,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment