Skip to content

Commit

Permalink
Allow user to pick any file for file transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
markwinter committed Sep 27, 2014
1 parent 8bd1543 commit eba2b3e
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 12 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<uses-sdk
android:minSdkVersion="8"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void onCreate(Bundle savedInstanceState) {
friendID.setText(uri.getHost());
//TODO: ACCEPT DNS LOOKUPS FROM URI

} else if (intent.getAction().equals("toxv2")) {
} else if (intent.getAction() == "toxv2") {
//else if it came from toxv2 restart

friendID.setText(intent.getStringExtra("originalUsername"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected void onCreate(Bundle savedInstanceState) {
Intent createAccount = new Intent(getApplicationContext(), CreateAcccountActivity.class);
startActivity(createAccount);
finish();

} else if(preferences.getBoolean("loggedin", false)) {
/* Attempt to start service in case it's not running */
Intent startTox = new Intent(getApplicationContext(), ToxDoService.class);
Expand Down
32 changes: 22 additions & 10 deletions app/src/main/java/im/tox/antox/fragments/ChatFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

Expand All @@ -38,6 +39,7 @@
import im.tox.antox.utils.AntoxFriend;
import im.tox.antox.utils.ChatMessages;
import im.tox.antox.utils.Constants;
import im.tox.antox.utils.FileDialog;
import im.tox.antox.utils.FriendInfo;
import im.tox.antox.utils.IconColor;
import im.tox.antox.utils.Tuple;
Expand Down Expand Up @@ -365,6 +367,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
toxSingleton.sendFileSendRequest(path, this.activeKey, getActivity());
}
}

if(requestCode==Constants.PHOTO_RESULT && resultCode==Activity.RESULT_OK){

if(photoPath!=null) {
Expand All @@ -373,6 +376,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
}

}

if(requestCode == Constants.FILE_RESULT && resultCode == Activity.RESULT_OK) {

}
}


Expand All @@ -382,14 +389,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

Runnable load = new Runnable() {
@Override
public void run() {

}
};

View rootView = inflater.inflate(R.layout.fragment_chat, container, false);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
Cursor cursor = getCursor();
Expand Down Expand Up @@ -486,7 +485,8 @@ public void onClick(View v) {
final CharSequence items[];
items = new CharSequence[] {
getResources().getString(R.string.attachment_photo),
getResources().getString(R.string.attachment_takephoto)
getResources().getString(R.string.attachment_takephoto),
getResources().getString(R.string.attachment_file)
};
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
Expand All @@ -496,6 +496,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, Constants.IMAGE_RESULT);
break;

case 1:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String image_name = "Antoxpic" + new Date().toString();
Expand All @@ -517,6 +518,18 @@ public void onClick(DialogInterface dialogInterface, int i) {
photoPath = file.getAbsolutePath();
}
startActivityForResult(cameraIntent, Constants.PHOTO_RESULT);
break;

case 2:
File mPath = new File(Environment.getExternalStorageDirectory() + "//DIR//");
FileDialog fileDialog = new FileDialog(getActivity(), mPath);
fileDialog.addFileListener(new FileDialog.FileSelectedListener() {
public void fileSelected(File file) {
toxSingleton.sendFileSendRequest(file.getPath(), activeKey, getActivity());
}
});
fileDialog.showDialog();
break;
}
}
});
Expand All @@ -526,5 +539,4 @@ public void onClick(DialogInterface dialogInterface, int i) {
return rootView;
}


}
7 changes: 7 additions & 0 deletions app/src/main/java/im/tox/antox/tox/ToxDoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.os.PowerManager.WakeLock;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Log;

Expand Down Expand Up @@ -51,7 +53,12 @@ public void run() {

try {
Thread.sleep(toxSingleton.jTox.doToxInterval());
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
toxSingleton.jTox.doTox();
wakeLock.release();
} catch (Exception e) {
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/im/tox/antox/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class Constants {
//file sending
public static final int IMAGE_RESULT = 0;
public static final int PHOTO_RESULT = 1;
public static final int FILE_RESULT = 3;

// Message Types
public static final int MESSAGE_TYPE_OWN = 1;
Expand Down
144 changes: 144 additions & 0 deletions app/src/main/java/im/tox/antox/utils/FileDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package im.tox.antox.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

public class FileDialog {
private static final String PARENT_DIR = "..";
private final String TAG = getClass().getName();
private String[] fileList;
private File currentPath;

public interface FileSelectedListener {
void fileSelected(File file);
}

public interface DirectorySelectedListener {
void directorySelected(File directory);
}

private ListenerList<FileSelectedListener> fileListenerList = new ListenerList<FileDialog.FileSelectedListener>();
private ListenerList<DirectorySelectedListener> dirListenerList = new ListenerList<FileDialog.DirectorySelectedListener>();
private final Activity activity;
private boolean selectDirectoryOption;
private String fileEndsWith;

public FileDialog(Activity activity, File path) {
this.activity = activity;
if (!path.exists()) path = Environment.getExternalStorageDirectory();
loadFileList(path);
}

public Dialog createFileDialog() {
Dialog dialog = null;
AlertDialog.Builder builder = new AlertDialog.Builder(activity);

builder.setTitle(currentPath.getPath());
if (selectDirectoryOption) {
builder.setPositiveButton("Select directory", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG, currentPath.getPath());
fireDirectorySelectedEvent(currentPath);
}
});
}

builder.setItems(fileList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String fileChosen = fileList[which];
File chosenFile = getChosenFile(fileChosen);
if (chosenFile.isDirectory()) {
loadFileList(chosenFile);
dialog.cancel();
dialog.dismiss();
showDialog();
} else fireFileSelectedEvent(chosenFile);
}
});

dialog = builder.show();
return dialog;
}


public void addFileListener(FileSelectedListener listener) {
fileListenerList.add(listener);
}

public void showDialog() {
createFileDialog().show();
}

private void fireFileSelectedEvent(final File file) {
fileListenerList.fireEvent(new ListenerList.FireHandler<FileSelectedListener>() {
public void fireEvent(FileSelectedListener listener) {
listener.fileSelected(file);
}
});
}

private void fireDirectorySelectedEvent(final File directory) {
dirListenerList.fireEvent(new ListenerList.FireHandler<DirectorySelectedListener>() {
public void fireEvent(DirectorySelectedListener listener) {
listener.directorySelected(directory);
}
});
}

private void loadFileList(File path) {
this.currentPath = path;
List<String> r = new ArrayList<String>();
if (path.exists()) {
if (path.getParentFile() != null) r.add(PARENT_DIR);
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String filename) {
File sel = new File(dir, filename);
if (!sel.canRead()) return false;
if (selectDirectoryOption) return sel.isDirectory();
else {
boolean endsWith = fileEndsWith != null ? filename.toLowerCase().endsWith(fileEndsWith) : true;
return endsWith || sel.isDirectory();
}
}
};
String[] fileList1 = path.list(filter);
for (String file : fileList1) {
r.add(file);
}
}
fileList = (String[]) r.toArray(new String[]{});
}

private File getChosenFile(String fileChosen) {
if (fileChosen.equals(PARENT_DIR)) return currentPath.getParentFile();
else return new File(currentPath, fileChosen);
}
}

class ListenerList<L> {
private List<L> listenerList = new ArrayList<L>();

public interface FireHandler<L> {
void fireEvent(L listener);
}

public void add(L listener) {
listenerList.add(listener);
}

public void fireEvent(FireHandler<L> fireHandler) {
List<L> copy = new ArrayList<L>(listenerList);
for (L l : copy) {
fireHandler.fireEvent(l);
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

<!-- Chat Fragment -->
<string name="attachment_photo">Attach photo</string>
<string name="attachment_file">Attach File</string>
<string name="attachment_takephoto">Take photo</string>
<string name="message_copy">Copy message</string>
<string name="message_delete">Delete message</string>
Expand Down

1 comment on commit eba2b3e

@markwinter
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to have broken sending the other types of file transfers

Please sign in to comment.