有没有办法制作我可以从其他类调用的Dropbox API活动?我有一个下载部分,而不是重写每个类中的代码,我可以将它放在自己的类中,然后在需要时调用该类吗?
还有一种隐藏我的Dropbox API密钥的方法吗?
以下是我目前设置API的方法。必须有一种更安全的方式,因为我不想在节目中显示我的细节。
public static String APP_TYPE ="/FOLDER -- LOCATION-- FOR--DOWNLOADS";
public static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_TYPE;
public static File Dir = new File (path);
AndroidAuthSession session = buildSession();
static DropboxAPI<AndroidAuthSession> dropboxAPI;
private final String APP_KEY = "MY -- KEY";
private final String APP_ACCESS = "MY -- PASSWORD";
private final String TOKEN = "MY -- ACCESS -- TOKEN";
然后在我的 onCreate
中 Dir.mkdir();
dropboxAPI = new DropboxAPI<AndroidAuthSession>(session);
我用我的on click命令调用它。
DownloadFromDropboxFromPath(path + "downloadFileFromDropbox", +APP_TYPE +"MY.apk");
最后,这是我调用API的实际方式。
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_ACCESS);
AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
session.setOAuth2AccessToken(TOKEN);
return session;
}
static final int UploadFromSelectApp = 9501;
static final int UploadFromFilemanager = 9502;
public static String DropboxUploadPathFrom = "";
public static String DropboxUploadName = "";
public static String DropboxDownloadPathFrom = "";
public static String DropboxDownloadPathTo = "";
private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) {
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final String uploadPathF = uploadPathFrom;
final String uploadPathT = uploadPathTo;
Thread th = new Thread(new Runnable() {
public void run() {
File tmpFile = null;
try {
tmpFile = new File(uploadPathF);
} catch (Exception e) {
e.printStackTrace();
}
FileInputStream fis = null;
try {
fis = new FileInputStream(tmpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
} catch (Exception e) {
}
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}
private void DownloadFromDropboxFromPath(String downloadPathTo, final String downloadPathFrom) {
DropboxDownloadPathTo = downloadPathTo;
DropboxDownloadPathFrom = downloadPathFrom;
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Downloading Please wait ...", Toast.LENGTH_LONG).show();
Thread th = new Thread(new Runnable() {
public void run() {
final File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
if (file.exists()) file.delete();
try {
FileOutputStream outputStream = new FileOutputStream(file);
castingapplistview.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null);
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
showInterstitial();
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
promptInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(promptInstall);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
th.start();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == UploadFromFilemanager) {
final Uri currFileURI = intent.getData();
final String pathFrom = currFileURI.getPath();
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
Thread th = new Thread(new Runnable() {
public void run() {
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
UploadToDropboxFromPath(pathFrom, "/db-test/" + DropboxUploadName + pathFrom.substring(pathFrom.lastIndexOf('.')));
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
}
});
th.start();
}
if (requestCode == UploadFromSelectApp) {
Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
final Uri uri = intent.getData();
DropboxUploadPathFrom = getPath(getApplicationContext(), uri);
if (DropboxUploadPathFrom == null) {
DropboxUploadPathFrom = uri.getPath();
}
Thread th = new Thread(new Runnable() {
public void run() {
try {
final File file = new File(DropboxUploadPathFrom);
InputStream inputStream = getContentResolver().openInputStream(uri);
dropboxAPI.putFile("/db-test/" + DropboxUploadName + file.getName().substring(file.getName().lastIndexOf("."),
file.getName().length()), inputStream, file.length(), null, new ProgressListener() {
@Override
public long progressInterval() {
return 100;
}
@Override
public void onProgress(long arg0, long arg1) {
}
});
getMain().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
});
th.start();
}
super.onActivityResult(requestCode, resultCode, intent);
}
public String getPath(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
if (s != null) {
cursor.close();
return s;
}
} catch (Exception e) {
}
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
if (s != null) {
cursor.close();
return s;
}
} catch (Exception e) {
}
try {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
String s = cursor.getString(column_index);
cursor.close();
return s;
} finally {
if (cursor != null) {
cursor.close();
}
}
}
public castingapplistview getMain() {
return this;
}
}
目前,我在使用Dropbox API的每个活动中都拥有所有这些代码。当然,我可以进入自己的班级,确保隐藏我的钥匙?一如既往地谢谢。
答案 0 :(得分:2)
答案是否定的。无法在您的应用中完美隐藏您的密钥。您可以将它们隐藏在存储库中,但要隐藏拥有.apk文件的人更加困难。有人确定不够,会得到它们。话虽如此,有很多方法可以让它变得困难,包括使用NDK和JNI,有一个函数可以返回你的密钥,因为应用程序调用提供了正确的应用程序签名,你可以做的不仅仅是混淆c / c ++代码。
任何可以反编译你的应用程序,任何人,都可以获得你的密钥。将它放在gradle文件中,不会让想要它的人保证安全。它被编译成一个java类,即使在混淆时也可以找到它。
这是一篇不错的文章,可以指导您前进。
http://www.informit.com/articles/article.aspx?p=2268753&seqNum=4
答案 1 :(得分:1)
有没有办法制作我可以从其他类调用的Dropbox API活动?我有一个下载部分,而不是重写每个类中的代码,我可以将它放在自己的类中,然后在需要时调用该类吗?
是。您可以使用这些常量创建超类,并在需要时进行扩展。
还有办法隐藏我的Dropbox API密钥吗?
是的,您可以将它们放在build.gradle(module:app)
中android {
...
defaultConfig {
...
}
buildTypes {
release {
...
}
buildTypes.each {
it.buildConfigField 'String', 'MY_API_TOKEN_KEY', MyApiTokenValue
}
}
要在您的活动(或任何Java类)上使用它们,只需使用:
BuildConfig.MY_API_TOKEN_KEY
您可以在this repository上看到它。