Initial commit

This commit is contained in:
vanitasvitae 2015-05-10 13:38:24 +02:00
commit 172eb3b76f
20 changed files with 679 additions and 0 deletions

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vanitasvitae.sticktoalbum" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/sticktoalbum"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
<data android:mimeType="video/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>
</manifest>

View File

@ -0,0 +1,430 @@
package de.vanitasvitae.sticktoalbum;
import android.app.AlertDialog;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
/**
* "Stick" your photos and videos into albums by moving them into new folders and updating
* Androids MediaStore.
* @author vanitas
*/
public class MainActivity extends ActionBarActivity
{
private ArrayList<Uri> sharedMedia;
private static String Tag = "stickToAlbum";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String appDirectoryPath = sharedPref.getString(SettingsActivity.KEY_PREF_APP_PATH, "");
if(appDirectoryPath.isEmpty())
{
appDirectoryPath = resetAppDirectoryPath();
}
sharedMedia = new ArrayList<Uri>();
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d(Tag, "Intent: " + action + ", " + type);
if (Intent.ACTION_SEND.equals(action) && type != null)
{
if (type.startsWith("image/") || type.startsWith("video/"))
{
handleSendSingleFile(intent);
}
}
else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null)
{
handleSendMultipleFiles(intent);
}
else
{
handleStartFromHomeScreen();
}
}
/**
* Handle one single file being shared with the application.
* Add the files Uri to the list of files to move and open the saveDialog.
* @param intent
*/
void handleSendSingleFile(Intent intent) {
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
sharedMedia.add(imageUri);
}
openSaveDialog();
}
/**
* Handle multiple files being shared with the application.
* Add the files Uris to the list of files to move and open the saveDialog.
* @param intent
*/
void handleSendMultipleFiles(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
sharedMedia = imageUris;
}
openSaveDialog();
}
/**
* Show Dialog to inform the user about the fact, that the feature he/she wants to use is not
* implemented
*/
void handleUnimplementedFeature()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_title_unimplemented_feature);
builder.setMessage(R.string.dialog_content_unimplemented_feature);
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
finish();
}
});
builder.show();
}
/**
* Open a Dialog that prompts the user to enter an album name.
* Reopens dialog, if album name is empty.
* Closes app after moving files or when canceled.
*/
private void openSaveDialog()
{
final Context context = this;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getText(R.string.dialog_title_album_name));
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.save_dialog, null);
builder.setView(dialogView);
final EditText input = (EditText) dialogView.findViewById(R.id.album_name);
input.setInputType(InputType.TYPE_CLASS_TEXT);
ListView existingAlbums = (ListView) dialogView.findViewById(R.id.album_list);
final ArrayList<String> directoryList = getFoldersOnDirectory(getAppDirectoryPath());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, directoryList);
existingAlbums.setAdapter(adapter);
existingAlbums.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
input.setText(directoryList.get(position));
}
});
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
String aName = input.getText().toString();
if(!aName.isEmpty())
{
moveFiles(aName);
finish();
}
else
{
Toast.makeText(context, getResources().getText(R.string.toast_invalid_album_name), Toast.LENGTH_SHORT).show();
openSaveDialog();
}
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
finish();
}
});
builder.show();
}
/**
* Show dialog that informs the user about how to use the app.
* Close app when closed.
*/
private void handleStartFromHomeScreen()
{
final Context context = this;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getText(R.string.dialog_title_on_start_from_home_screen));
builder.setMessage(getResources().getText(R.string.dialog_content_on_start_from_home_screen));
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
finish();
}
});
builder.setNegativeButton(R.string.action_settings, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent intent = new Intent(context, SettingsActivity.class);
startActivity(intent);
//handleUnimplementedFeature();
}
});
builder.show();
}
/**
* Move selected files to a new album. The album is located at appDirectoryPath/albumName
* @param albumName
*/
private void moveFiles(String albumName)
{
for(Uri u: sharedMedia)
{
String srcDest = getRealPathFromURI(this, u);
if(!moveImageFromTo(srcDest, calcTargetPath(albumName, srcDest)))
{
System.out.println("Fail: " +srcDest);
}
}
}
/**
* Move file src to target.
* @param src
* @param target
* @return
*/
private boolean moveImageFromTo(String src, String target)
{
if(!src.equals(target))
new LoadInitialImageData().doInBackground(src,target);
return true;
}
private void deleteFileFromMediaStore(String path)
{
if(getMimeType(path).startsWith("image/"))
{
String[] retCol = {MediaStore.Images.Media._ID};
Cursor cur = this.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
retCol,
MediaStore.MediaColumns.DATA+"='"+path+"'", null, null);
if (cur.getCount() == 0)
{
Log.e(Tag, "Could not find image "+path+". Therefore can't delete file from MediaStore");
return;
}
cur.moveToFirst();
int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID));
cur.close();
Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
this.getContentResolver().delete(uri, null, null);
Log.v(Tag, "Deleted image "+path+" from MediaStore as "+uri);
}
else if(getMimeType(path).startsWith("video/"))
{
String[] retCol = {MediaStore.Video.Media._ID};
Cursor cur = this.getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
retCol,
MediaStore.MediaColumns.DATA+"='"+path+"'",null,null);
if(cur.getCount() == 0)
{
Log.e(Tag, "Could not find video "+path+". Therefore can't delete file from MediaStore");
return;
}
cur.moveToFirst();
int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID));
cur.close();
Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
this.getContentResolver().delete(uri, null, null);
Log.v(Tag, "Deleted video "+path+" from MediaStore as "+uri);
}
}
private void addFileToMediaStore(String path)
{
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{path},
null,
new MediaScannerConnection.OnScanCompletedListener()
{
@Override
public void onScanCompleted(String path, Uri uri)
{
Log.v(Tag, "File "+path+" was scanned successfully: "+uri);
}
});
}
private String calcTargetPath(String albumName, String srcDest)
{
String file = getAppDirectoryPath()+albumName+srcDest.substring(srcDest.lastIndexOf("/"));
Log.v(Tag, "Path for "+srcDest+" in "+albumName+" will be "+file);
return file;
}
public String resetAppDirectoryPath()
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Albums/";
Log.d(Tag, "AppDirectoryPath = "+path);
SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefs.putString(SettingsActivity.KEY_PREF_APP_PATH, path);
prefs.apply();
return path;
}
public String getAppDirectoryPath()
{
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String appDirectoryPath = sharedPref.getString(SettingsActivity.KEY_PREF_APP_PATH, "");
if(!appDirectoryPath.isEmpty()) return appDirectoryPath;
else
{
return resetAppDirectoryPath();
}
}
public ArrayList<String> getFoldersOnDirectory(String dirPath)
{
File parent = new File(dirPath);
File[] cont = parent.listFiles();
ArrayList<String> subDirs = new ArrayList<>();
if(cont != null)
{
for (File f : cont)
{
if (f.isDirectory()) subDirs.add(f.getName());
}
}
return subDirs;
}
public String getRealPathFromURI(Context context, Uri contentUri)
{
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String realPath = cursor.getString(column_index);
cursor.close();
Log.v(Tag, "Real path for file "+contentUri+" is "+realPath);
return realPath;
}
public static String getMimeType(String url)
{
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
/**
* String[] = {src, target}
*/
private class LoadInitialImageData extends AsyncTask<String, Void, Void>
{
@Override
protected Void doInBackground(String... params)
{
InputStream in = null;
OutputStream out = null;
String src = params[0];
String target = params[1];
try
{
File dir = new File(src.substring(0, src.lastIndexOf("/")));
if (!dir.exists())
dir.mkdirs();
in = new FileInputStream(src);
File outFile = new File(target.substring(0, target.lastIndexOf("/")));
if (!outFile.exists())
outFile.mkdirs();
out = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
addFileToMediaStore(target);
deleteFileFromMediaStore(src);
} catch (FileNotFoundException e)
{
e.printStackTrace();
return null;
} catch (IOException e)
{
e.printStackTrace();
return null;
}
return null;
}
@Override
protected void onPostExecute(Void v)
{
}
}
}

View File

@ -0,0 +1,38 @@
package de.vanitasvitae.sticktoalbum;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
/**
* Created by vanitas on 10.05.15.
*/
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
{
public final static String KEY_PREF_APP_PATH = "pref_app_path";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.settings);
getPreferenceScreen().getSharedPreferences()
.registerOnSharedPreferenceChangeListener(this);
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(KEY_PREF_APP_PATH)) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String appDirectoryPath = sharedPref.getString(SettingsActivity.KEY_PREF_APP_PATH, "");
if(appDirectoryPath.isEmpty())
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Pictures/Albums/";
SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefs.putString(SettingsActivity.KEY_PREF_APP_PATH, path);
prefs.apply();
}
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -0,0 +1,17 @@
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/sticktoalbum"
/>
</RelativeLayout>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lin_layout">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/album_name"
android:hint="@string/hint_album_name"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/album_list">
</ListView>
</LinearLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<EditTextPreference
android:key="pref_app_path"
android:title="@string/pref_title_app_path"
android:summary="@string/pref_description_app_path"
android:defaultValue="" />
</PreferenceScreen>

View File

@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 601 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 446 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 735 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -0,0 +1,17 @@
<resources>
<string name="app_name">StickToAlbum</string>
<string name="dialog_title_album_name">Albumnamen eingeben</string>
<string name="dialog_title_on_start_from_home_screen">Teile Fotos mit der App!</string>
<string name="dialog_content_on_start_from_home_screen">Teile Fotos (oder Videos) mit StickToAlbum, um diese in ein Album zu verschieben.</string>
<string name="dialog_title_unimplemented_feature">Funktion nicht verfügbar</string>
<string name="dialog_content_unimplemented_feature">Diese Funktion ist noch nicht fertig implementiert.\nSchau mal nach Updates :)</string>
<string name="toast_invalid_album_name">Bitte gebe einen Albumnamen ein!</string>
<string name="action_settings">Einstellungen</string>
<string name="ok">OK</string>
<string name="cancel">Abbrechen</string>
<string name="hint_album_name">Neues Album</string>
<string name="pref_title_app_path">Speicherort</string>
<string name="pref_description_app_path">Wo sollen neue Alben angelegt werden? Um zurückzusetzen, lösche diesen Wert und starte die App neu.</string>
</resources>

View File

@ -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>

View File

@ -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>

View File

@ -0,0 +1,17 @@
<resources>
<string name="app_name">StickToAlbum</string>
<string name="dialog_title_album_name">Enter Album Name</string>
<string name="dialog_title_on_start_from_home_screen">Share Images with the app!</string>
<string name="dialog_content_on_start_from_home_screen">In order to stick photos in albums, you have to share those photos (or videos) with this app.</string>
<string name="dialog_title_unimplemented_feature">Feature unavailable</string>
<string name="dialog_content_unimplemented_feature">This feature is not implemented yet.\nCheck out updates :)</string>
<string name="toast_invalid_album_name">Please enter valid album name!</string>
<string name="action_settings">Settings</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="hint_album_name">New Album</string>
<string name="pref_title_app_path">Album Directory</string>
<string name="pref_description_app_path">In which directory are Albums located? Delete value and restart app to reset.</string>
</resources>

View File

@ -0,0 +1,61 @@
<resources>
<string name="title_activity_settings">Settings</string>
<!-- Strings related to Settings -->
<!-- Example General settings -->
<string name="pref_header_general">General</string>
<string name="pref_title_social_recommendations">Enable social recommendations</string>
<string name="pref_description_social_recommendations">Recommendations for people to contact
based on your message history
</string>
<string name="pref_title_display_name">Display name</string>
<string name="pref_default_display_name">John Smith</string>
<string name="pref_title_add_friends_to_messages">Add friends to messages</string>
<string-array name="pref_example_list_titles">
<item>Always</item>
<item>When possible</item>
<item>Never</item>
</string-array>
<string-array name="pref_example_list_values">
<item>1</item>
<item>0</item>
<item>-1</item>
</string-array>
<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data &amp; sync</string>
<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>-1</item>
</string-array>
<string name="pref_title_system_sync_settings">System sync settings</string>
<!-- Example settings for Notifications -->
<string name="pref_header_notifications">Notifications</string>
<string name="pref_title_new_message_notifications">New message notifications</string>
<string name="pref_title_ringtone">Ringtone</string>
<string name="pref_ringtone_silent">Silent</string>
<string name="pref_title_vibrate">Vibrate</string>
</resources>

View File

@ -0,0 +1,8 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
</style>
</resources>

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB