From afe2730fbad3e955a8342b5274912631bb5d830e Mon Sep 17 00:00:00 2001 From: Rachel030219 <291054446@qq.com> Date: Sun, 23 Oct 2016 07:24:25 +0800 Subject: [PATCH] I forgot to add the java files! Damn it --- .../hashchecker/AboutActivity.java | 123 ++++++ .../hashchecker/ClipboardManager.java | 55 +++ .../rachel030219/hashchecker/FileUtils.java | 158 +++++++ .../rachel030219/hashchecker/HashTool.java | 96 +++++ .../hashchecker/MainActivity.java | 408 ++++++++++++++++++ .../hashchecker/SettingsActivity.java | 57 +++ .../hashchecker/SettingsFragment.java | 28 ++ 7 files changed, 925 insertions(+) create mode 100644 app/src/main/java/net/rachel030219/hashchecker/AboutActivity.java create mode 100644 app/src/main/java/net/rachel030219/hashchecker/ClipboardManager.java create mode 100644 app/src/main/java/net/rachel030219/hashchecker/FileUtils.java create mode 100644 app/src/main/java/net/rachel030219/hashchecker/HashTool.java create mode 100644 app/src/main/java/net/rachel030219/hashchecker/MainActivity.java create mode 100644 app/src/main/java/net/rachel030219/hashchecker/SettingsActivity.java create mode 100644 app/src/main/java/net/rachel030219/hashchecker/SettingsFragment.java diff --git a/app/src/main/java/net/rachel030219/hashchecker/AboutActivity.java b/app/src/main/java/net/rachel030219/hashchecker/AboutActivity.java new file mode 100644 index 0000000..200a573 --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/AboutActivity.java @@ -0,0 +1,123 @@ +/* + Copyright 2016 Rachel030219 + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package net.rachel030219.hashchecker; + +import android.os.Build; +import android.os.Bundle; +import android.net.Uri; +import android.view.View; +import android.view.MenuItem; +import android.view.WindowManager; +import android.widget.TextView; +import android.content.Intent; +import android.content.Context; +import android.content.DialogInterface; +import android.content.res.Resources; + +import android.support.v7.app.ActionBar; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; + +public class AboutActivity extends AppCompatActivity{ + TextView mPreferenceFragmentCompat; + TextView mMaterialSettingsActivityDemo; + + TextView mAboutMe; + + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + setContentView(R.layout.about); + + Toolbar mToolbar = (Toolbar)findViewById(R.id.about_toolbar); + mToolbar.setTitle(getTitle()); + setSupportActionBar(mToolbar); + + ActionBar mActionBar = getSupportActionBar(); + mActionBar.setHomeButtonEnabled(true); + mActionBar.setDisplayHomeAsUpEnabled(true); + + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { + WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes(); + localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags); + } + + bindViews(); + } + + private void bindViews(){ + Resources res = getResources(); + mPreferenceFragmentCompat = (TextView)findViewById(R.id.about_projects_preferencefragment); + mPreferenceFragmentCompat.setOnClickListener(new OnProjectClick(this,res.getString(R.string.about_projects_preferencefragment),res.getString(R.string.about_projects_preferencefragment_license),"https://github.com/Machinarius/PreferenceFragment-Compat")); + mMaterialSettingsActivityDemo = (TextView)findViewById(R.id.about_projects_materialsettingsactivitydemo); + mMaterialSettingsActivityDemo.setOnClickListener(new OnProjectClick(this,res.getString(R.string.about_projects_materialsettingsactivitydemo),res.getString(R.string.about_projects_materialsettingsactivitydemo_license),"https://drakeet.me/material-design-settings-activity")); + + mAboutMe = (TextView)findViewById(R.id.about_me); + mAboutMe.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View v){ + AlertDialog.Builder builder = new AlertDialog.Builder(AboutActivity.this); + builder.setTitle(R.string.about_me_title); + builder.setMessage(R.string.about_me_content); + builder.setPositiveButton("GOT IT",null); + builder.show(); + } + }); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item){ + // TODO: Implement this method + switch(item.getItemId()){ + case android.R.id.home: + finish(); + break; + } + return true; + } + + class OnProjectClick implements View.OnClickListener{ + Context context; + String title; + String license; + String url; + + OnProjectClick(Context context,String title,String license,String url){ + this.context = context; + this.title = title; + this.license = license; + this.url = url; + } + + @Override + public void onClick(View v){ + AlertDialog.Builder dialog = new AlertDialog.Builder(context); + dialog.setTitle(title); + dialog.setMessage(license); + dialog.setPositiveButton("GOT IT",null); + dialog.setNegativeButton("MORE...",new DialogInterface.OnClickListener(){ + @Override + public void onClick(DialogInterface dialog,int i){ + Uri uri = Uri.parse(url); + Intent intent = new Intent(Intent.ACTION_VIEW,uri); + startActivity(intent); + } + }); + dialog.show(); + } + } +} diff --git a/app/src/main/java/net/rachel030219/hashchecker/ClipboardManager.java b/app/src/main/java/net/rachel030219/hashchecker/ClipboardManager.java new file mode 100644 index 0000000..04408e3 --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/ClipboardManager.java @@ -0,0 +1,55 @@ +/* + Copyright 2016 Rachel030219 + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package net.rachel030219.hashchecker; + +public class ClipboardManager{ + android.text.ClipboardManager oldManager; + android.content.ClipboardManager newManager; + + boolean newAPI = android.os.Build.VERSION.SDK_INT >= 11; + + public ClipboardManager(android.content.Context context){ + if(newAPI){ + newManager = (android.content.ClipboardManager)context.getSystemService(context.CLIPBOARD_SERVICE); + } else { + oldManager = (android.text.ClipboardManager)context.getSystemService(context.CLIPBOARD_SERVICE); + } + } + + public void set(String text){ + if(newAPI){ + newManager.setPrimaryClip(android.content.ClipData.newPlainText(null, text)); + } else { + oldManager.setText(text); + } + } + + public CharSequence get(){ + if(newAPI){ + return newManager.getPrimaryClip().getItemAt(0).getText(); + } else { + return oldManager.getText(); + } + } + + public boolean has(){ + if(newAPI){ + return newManager.hasPrimaryClip(); + } else { + return oldManager.hasText(); + } + } +} diff --git a/app/src/main/java/net/rachel030219/hashchecker/FileUtils.java b/app/src/main/java/net/rachel030219/hashchecker/FileUtils.java new file mode 100644 index 0000000..5af2bd0 --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/FileUtils.java @@ -0,0 +1,158 @@ +/* + Copyright 2016 Rachel030219 + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +// From Rachel:I got this from a answer under Stack Overflow. Thanks him! + +package net.rachel030219.hashchecker; + +import android.os.Build; +import android.os.Environment; +import android.net.Uri; +import android.content.Context; +import android.content.ContentUris; +import android.database.Cursor; +import android.provider.MediaStore; +import android.provider.DocumentsContract; + +public class FileUtils{ + /** + * Get a file path from a Uri. This will get the the path for Storage Access + * Framework Documents, as well as the _data field for the MediaStore and + * other file-based ContentProviders. + * + * @param context The context. + * @param uri The Uri to query. + * @author paulburke + */ + 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]; + } + + // TODO handle non-primary volumes + } + // 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; + } + + /** + * 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 = "_data"; + final String[] projection = { + column + }; + + try { + cursor = context.getContentResolver().query(uri, projection, 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 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()); + } + + /** + * @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()); + } +} diff --git a/app/src/main/java/net/rachel030219/hashchecker/HashTool.java b/app/src/main/java/net/rachel030219/hashchecker/HashTool.java new file mode 100644 index 0000000..2236feb --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/HashTool.java @@ -0,0 +1,96 @@ +/* + Copyright 2016 Rachel030219 + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package net.rachel030219.hashchecker; + +import java.io.File; +import java.io.FileInputStream; +import java.security.MessageDigest; +import java.security.DigestInputStream; + +public class HashTool{ + + public static String getFileHash(String type, File file){ + + int bufferSize = 256 * 1024; + + FileInputStream fileInputStream = null; + + DigestInputStream digestInputStream = null; + + try { + + MessageDigest messageDigest = MessageDigest.getInstance(type); + + fileInputStream = new FileInputStream(file); + + digestInputStream = new DigestInputStream(fileInputStream,messageDigest); + + byte[] buffer =new byte[bufferSize]; + + while (digestInputStream.read(buffer) > 0); + + messageDigest= digestInputStream.getMessageDigest(); + + byte[] resultByteArray = messageDigest.digest(); + + return byteArrayToHex(resultByteArray); + + } catch (Exception e) { + + return null; + + } finally { + + try { + + digestInputStream.close(); + + } catch (Exception e) { + + } + + try { + + fileInputStream.close(); + + } catch (Exception e) { + + } + } + + } + + public static String byteArrayToHex(byte[] byteArray) { + + char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' }; + + char[] resultCharArray =new char[byteArray.length * 2]; + + int index = 0; + + for (byte b : byteArray) { + + resultCharArray[index++] = hexDigits[b>>> 4 & 0xf]; + + resultCharArray[index++] = hexDigits[b& 0xf]; + + } + + return new String(resultCharArray); + + } + +} diff --git a/app/src/main/java/net/rachel030219/hashchecker/MainActivity.java b/app/src/main/java/net/rachel030219/hashchecker/MainActivity.java new file mode 100644 index 0000000..37a78cc --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/MainActivity.java @@ -0,0 +1,408 @@ +package net.rachel030219.hashchecker; + +import java.io.File; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import android.Manifest; +import android.os.Build; +import android.os.Bundle; +import android.app.ProgressDialog; +import android.net.Uri; +import android.text.Editable; +import android.text.TextWatcher; +import android.view.View; +import android.view.MenuItem; +import android.view.WindowManager; +import android.widget.EditText; +import android.widget.TextView; +import android.content.Intent; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.res.Resources; +import android.graphics.Color; +import android.preference.PreferenceManager; + +import android.support.v4.widget.DrawerLayout; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.app.AlertDialog; +import android.support.v7.app.ActionBarDrawerToggle; +import android.support.v7.widget.Toolbar; +import android.support.v7.widget.CardView; +import android.support.design.widget.Snackbar; +import android.support.design.widget.NavigationView; +import android.support.design.widget.CoordinatorLayout; +import android.support.design.widget.FloatingActionButton; + +public class MainActivity extends AppCompatActivity { + private static final int FILE_SELECT_CODE = 1; + private static final int REQUEST_FILE_PERMISSION_CODE_INAPP = 2; + private static final int REQUEST_FILE_PERMISSION_CODE_SHARE = 3; + + Toolbar mToolbar; + ActionBar mActionBar; + DrawerLayout mDrawerLayout; + ActionBarDrawerToggle mDrawerToggle; + FloatingActionButton mFab; + CoordinatorLayout mRoot; + + CardView mResult; + TextView mFile; + TextView mMD5; + TextView mSHA1; + TextView mSHA256; + TextView mSHA384; + TextView mSHA512; + + String md5 = null; + String sha1 = null; + String sha256 = null; + String sha384 = null; + String sha512 = null; + + CardView mCheck; + EditText mCheckInput; + + ClipboardManager manager; + + SharedPreferences defaultPreferences; + boolean uppercase; + + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + + setContentView(R.layout.main); + + manager = new ClipboardManager(this); + + bindView(); + checkUpdated(); + + // handle share + Intent open = getIntent(); + if(open != null){ + String action = open.getAction(); + String type = open.getType(); + if(action.equals(Intent.ACTION_SEND) && type != null){ + defaultPreferences = PreferenceManager.getDefaultSharedPreferences(this); + uppercase = defaultPreferences.getBoolean("output_case",true); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_FILE_PERMISSION_CODE_SHARE); + } else { + Uri data = open.getParcelableExtra(Intent.EXTRA_STREAM); + showResult(data); + } + } else { + Uri data = open.getParcelableExtra(Intent.EXTRA_STREAM); + showResult(data); + } + } + } + } + + private void bindView(){ + mToolbar = (Toolbar)findViewById(R.id.toolbar); + mToolbar.setTitle(getTitle()); + setSupportActionBar(mToolbar); + + mActionBar = getSupportActionBar(); + mActionBar.setHomeButtonEnabled(true); + mActionBar.setDisplayHomeAsUpEnabled(true); + + mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer); + mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open, R.string.close) { + @Override + public void onDrawerOpened(View mDrawerView) { + super.onDrawerOpened(mDrawerView); + } + @Override + public void onDrawerClosed(View mDrawerView) { + super.onDrawerClosed(mDrawerView); + } + }; + mDrawerToggle.syncState(); + mDrawerLayout.setDrawerListener(mDrawerToggle); + + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { + WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes(); + localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags); + mDrawerLayout.setFitsSystemWindows(true); + mDrawerLayout.setClipToPadding(false); + } + + NavigationView mNavigation = (NavigationView) findViewById(R.id.navigation); + mNavigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { + @Override + public boolean onNavigationItemSelected(MenuItem menuItem) { + switch(menuItem.getItemId()){ + case R.id.setting: + Intent settingIntent = new Intent(MainActivity.this,SettingsActivity.class); + startActivity(settingIntent); + break; + case R.id.about: + Intent aboutIntent = new Intent(MainActivity.this,AboutActivity.class); + startActivity(aboutIntent); + break; + } + mDrawerLayout.closeDrawers(); + return true; + } + }); + + mFab = (FloatingActionButton)findViewById(R.id.fab); + mFab.setOnClickListener(new View.OnClickListener(){ + @Override + public void onClick(View v){ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ + if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_FILE_PERMISSION_CODE_INAPP); + } else { + showFileChooser(); + } + } else { + showFileChooser(); + } + } + }); + + mRoot = (CoordinatorLayout)findViewById(R.id.rootLayout); + + mResult = (CardView)findViewById(R.id.result); + mFile = (TextView)findViewById(R.id.file); + mMD5 = (TextView)findViewById(R.id.md5); + mSHA1 = (TextView)findViewById(R.id.sha1); + mSHA256 = (TextView)findViewById(R.id.sha256); + mSHA384 = (TextView)findViewById(R.id.sha384); + mSHA512 = (TextView)findViewById(R.id.sha512); + + mCheck = (CardView)findViewById(R.id.check); + mCheckInput = (EditText)findViewById(R.id.checkInput); + mCheckInput.addTextChangedListener(new TextWatcher(){ + @Override + public void beforeTextChanged(CharSequence text, int start, int count, int after){ + // TODO: Implement this method + } + + @Override + public void onTextChanged(CharSequence text, int start, int before, int count){ + // TODO: Implement this method + } + + @Override + public void afterTextChanged(Editable text){ + // TODO: Implement this method + String check = null; + Pattern lowerPattern = Pattern.compile("[a-z]"); + Pattern upperPattern = Pattern.compile("[A-Z]"); + if(!text.toString().equals("")) + check = text.toString().substring(0,text.toString().length()); + if(uppercase){ + if(check != null){ + Matcher matcher = lowerPattern.matcher(check); + if(matcher.find()){ + int selection = mCheckInput.getSelectionStart(); + mCheckInput.setText(text.toString().toUpperCase()); + mCheckInput.setSelection(selection); + } + } + } else { + if(check != null){ + Matcher matcher = upperPattern.matcher(check); + if(matcher.find()){ + int selection = mCheckInput.getSelectionStart(); + mCheckInput.setText(text.toString().toLowerCase()); + mCheckInput.setSelection(selection); + } + } + } + + if(!text.toString().equals(md5) && !text.toString().equals(sha1) && !text.toString().equals(sha256) && !text.toString().equals(sha384) && !text.toString().equals(sha512)) { + mCheckInput.setTextColor(Color.parseColor("#FF0000")); + mMD5.setTextColor(Color.parseColor("#797979")); + mSHA1.setTextColor(Color.parseColor("#797979")); + mSHA256.setTextColor(Color.parseColor("#797979")); + mSHA384.setTextColor(Color.parseColor("#797979")); + mSHA512.setTextColor(Color.parseColor("#797979")); + } else { + if(text.toString().equals(md5)) { + mMD5.setTextColor(Color.parseColor("#00CD00")); + mCheckInput.setTextColor(Color.parseColor("#00CD00")); + } else if(text.toString().equals(sha1)) { + mSHA1.setTextColor(Color.parseColor("#00CD00")); + mCheckInput.setTextColor(Color.parseColor("#00CD00")); + } else if(text.toString().equals(sha256)) { + mSHA256.setTextColor(Color.parseColor("#00CD00")); + mCheckInput.setTextColor(Color.parseColor("#00CD00")); + } else if(text.toString().equals(sha384)) { + mSHA384.setTextColor(Color.parseColor("#00CD00")); + mCheckInput.setTextColor(Color.parseColor("#00CD00")); + } else if(text.toString().equals(sha512)) { + mSHA512.setTextColor(Color.parseColor("#00CD00")); + mCheckInput.setTextColor(Color.parseColor("#00CD00")); + } + } + } + }); + } + + private void showFileChooser() { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("*/*"); + intent.addCategory(Intent.CATEGORY_OPENABLE); + + defaultPreferences = PreferenceManager.getDefaultSharedPreferences(this); + uppercase = defaultPreferences.getBoolean("output_case",true); + + try { + startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE); + mCheckInput.setText(""); + } catch (android.content.ActivityNotFoundException ex) { + android.widget.Toast.makeText(this, "Please install a File Manager.", android.widget.Toast.LENGTH_SHORT).show(); + } + } + + public void checkUpdated(){ + final SharedPreferences preferences = getSharedPreferences("updated",MODE_PRIVATE); + if(!preferences.getBoolean("updated12",false)){ + AlertDialog.Builder dialog = new AlertDialog.Builder(this); + dialog.setTitle(R.string.updated_title); + dialog.setMessage(R.string.updated_changelog); + dialog.setPositiveButton("GOT IT",new DialogInterface.OnClickListener(){ + @Override + public void onClick(DialogInterface dialog,int count){ + preferences.edit().putBoolean("updated12",true).apply(); + } + }); + dialog.show(); + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data){ + switch(requestCode){ + case FILE_SELECT_CODE: + if(resultCode == RESULT_OK){ + showResult(data.getData()); + } + break; + } + super.onActivityResult(requestCode,resultCode,data); + } + + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if(requestCode == REQUEST_FILE_PERMISSION_CODE_INAPP){ + int grantResult = grantResults[0]; + if (grantResult == PackageManager.PERMISSION_GRANTED){ + showFileChooser(); + } + } else if(requestCode == REQUEST_FILE_PERMISSION_CODE_SHARE) { + int grantResult = grantResults[0]; + if (grantResult == PackageManager.PERMISSION_GRANTED){ + showResult((Uri)getIntent().getParcelableExtra(Intent.EXTRA_STREAM)); + } + } + + int grantResult = grantResults[0]; + if (grantResult == PackageManager.PERMISSION_GRANTED){ + switch(requestCode){ + case REQUEST_FILE_PERMISSION_CODE_INAPP: + showFileChooser(); + break; + case REQUEST_FILE_PERMISSION_CODE_SHARE: + showResult((Uri)getIntent().getParcelableExtra(Intent.EXTRA_STREAM)); + break; + } + } else if(grantResult == PackageManager.PERMISSION_DENIED) { + Snackbar.make(mRoot,"Pl",Snackbar.LENGTH_LONG).show(); + } + } + + public void showResult(Uri uri){ + try{ + final File file = new File(FileUtils.getPath(this,uri)); + final Resources res = getResources(); + + final ProgressDialog dialog = ProgressDialog.show(MainActivity.this,null,"Calculating...",true); + new Thread(){ + @Override + public void run(){ + md5 = HashTool.getFileHash("MD5",file); + sha1 = HashTool.getFileHash("SHA1",file); + sha256 = HashTool.getFileHash("SHA256",file); + sha384 = HashTool.getFileHash("SHA384",file); + sha512 = HashTool.getFileHash("SHA512",file); + + if(uppercase){ + md5 = md5.toUpperCase(); + sha1 = sha1.toUpperCase(); + sha256 = sha256.toUpperCase(); + sha384 = sha384.toUpperCase(); + sha512 = sha512.toUpperCase(); + } else { + md5 = md5.toLowerCase(); + sha1 = sha1.toLowerCase(); + sha256 = sha256.toLowerCase(); + sha384 = sha384.toLowerCase(); + sha512 = sha512.toLowerCase(); + } + + runOnUiThread(new Runnable(){ + @Override + public void run(){ + changeResults(); + + mFile.setText(String.format(res.getString(R.string.file),file.getAbsolutePath())); + + mResult.setVisibility(View.VISIBLE); + mCheck.setVisibility(View.VISIBLE); + mCheckInput.requestFocus(); + mCheckInput.setFocusableInTouchMode(true); + + dialog.dismiss(); + } + }); + } + }.start(); + } catch (Exception e) { + + } + } + + public void changeResults(){ + Resources res = getResources(); + mMD5.setText(String.format(res.getString(R.string.md5),md5)); + mSHA1.setText(String.format(res.getString(R.string.sha1),sha1)); + mSHA256.setText(String.format(res.getString(R.string.sha256),sha256)); + mSHA384.setText(String.format(res.getString(R.string.sha384),sha384)); + mSHA512.setText(String.format(res.getString(R.string.sha512),sha512)); + + mMD5.setOnLongClickListener(new OnHashLongClick(md5,"MD5")); + mSHA1.setOnLongClickListener(new OnHashLongClick(sha1,"SHA1")); + mSHA256.setOnLongClickListener(new OnHashLongClick(sha256,"SHA256")); + mSHA384.setOnLongClickListener(new OnHashLongClick(sha384,"SHA384")); + mSHA512.setOnLongClickListener(new OnHashLongClick(sha512,"SHA512")); + } + + class OnHashLongClick implements View.OnLongClickListener{ + String text; + String type; + + OnHashLongClick(String text,String type){ + this.text = text; + this.type = type; + } + + @Override + public boolean onLongClick(View v){ + manager.set(text); + Snackbar.make(mRoot,String.format(getResources().getString(R.string.copied),type),Snackbar.LENGTH_SHORT).show(); + return true; + } + } +} diff --git a/app/src/main/java/net/rachel030219/hashchecker/SettingsActivity.java b/app/src/main/java/net/rachel030219/hashchecker/SettingsActivity.java new file mode 100644 index 0000000..41ae2d2 --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/SettingsActivity.java @@ -0,0 +1,57 @@ +/* + Copyright 2016 Rachel030219 + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package net.rachel030219.hashchecker; + +import android.os.Build; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.WindowManager; + +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; + +public class SettingsActivity extends AppCompatActivity{ + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + setContentView(R.layout.settings_activity); + + Toolbar mToolbar = (Toolbar)findViewById(R.id.settings_toolbar); + mToolbar.setTitle(getTitle()); + setSupportActionBar(mToolbar); + + ActionBar mActionBar = getSupportActionBar(); + mActionBar.setHomeButtonEnabled(true); + mActionBar.setDisplayHomeAsUpEnabled(true); + + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { + WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes(); + localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item){ + // TODO: Implement this method + switch(item.getItemId()){ + case android.R.id.home: + finish(); + break; + } + return true; + } +} diff --git a/app/src/main/java/net/rachel030219/hashchecker/SettingsFragment.java b/app/src/main/java/net/rachel030219/hashchecker/SettingsFragment.java new file mode 100644 index 0000000..3f827be --- /dev/null +++ b/app/src/main/java/net/rachel030219/hashchecker/SettingsFragment.java @@ -0,0 +1,28 @@ +/* + Copyright 2016 Rachel030219 + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + */ +package net.rachel030219.hashchecker; + +import android.os.Bundle; + +import com.github.machinarius.preferencefragment.PreferenceFragment; + +public class SettingsFragment extends PreferenceFragment{ + @Override + public void onCreate(Bundle savedInstanceState){ + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.layout.settings); + } +}