Commit de3f566b authored by Alexander Ivanov's avatar Alexander Ivanov

Implement chat export using DialogFragment.

parent 7be4685a
......@@ -14,14 +14,10 @@
*/
package com.xabber.android.ui;
import java.io.File;
import java.util.Collection;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.ContextMenu;
......@@ -38,7 +34,6 @@ import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import com.xabber.android.data.ActivityManager;
import com.xabber.android.data.Application;
......@@ -67,9 +62,7 @@ import com.xabber.android.data.notification.NotificationManager;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.ChatViewerAdapter;
import com.xabber.android.ui.adapter.OnTextChangedListener;
import com.xabber.android.ui.dialog.ConfirmDialogListener;
import com.xabber.android.ui.dialog.DialogBuilder;
import com.xabber.android.ui.dialog.ExportChatDialogBuilder;
import com.xabber.android.ui.dialog.ChatExportDialogFragment;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.android.ui.widget.PageSwitcher;
import com.xabber.android.ui.widget.PageSwitcher.OnSelectListener;
......@@ -87,8 +80,7 @@ import com.xabber.androiddev.R;
public class ChatViewer extends ManagedActivity implements
View.OnClickListener, View.OnKeyListener, OnSelectListener,
OnChatChangedListener, OnContactChangedListener,
OnAccountChangedListener, OnEditorActionListener,
ConfirmDialogListener, OnTextChangedListener {
OnAccountChangedListener, OnEditorActionListener, OnTextChangedListener {
/**
* Attention request.
......@@ -133,8 +125,6 @@ public class ChatViewer extends ManagedActivity implements
private static final int CONTEXT_MENU_COPY_ID = 0x102;
private static final int CONTEXT_MENU_REMOVE_ID = 0x103;
private static final int DIALOG_EXPORT_CHAT_ID = 0x200;
private ChatViewerAdapter chatViewerAdapter;
private PageSwitcher pageSwitcher;
......@@ -392,7 +382,9 @@ public class ChatViewer extends ManagedActivity implements
.setText("");
return true;
case OPTION_MENU_EXPORT_CHAT_ID:
showDialog(DIALOG_EXPORT_CHAT_ID);
ChatExportDialogFragment.newInstance(actionWithAccount,
actionWithUser).show(getSupportFragmentManager(),
"CHAT_EXPORT");
return true;
case OPTION_MENU_CALL_ATTENTION_ID:
try {
......@@ -540,18 +532,6 @@ public class ChatViewer extends ManagedActivity implements
return false;
}
@Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
switch (id) {
case DIALOG_EXPORT_CHAT_ID:
return new ExportChatDialogBuilder(this, DIALOG_EXPORT_CHAT_ID,
this, actionWithAccount, actionWithUser).create();
default:
return null;
}
}
@Override
public void onClick(View view) {
switch (view.getId()) {
......@@ -741,66 +721,6 @@ public class ChatViewer extends ManagedActivity implements
return false;
}
@Override
public void onAccept(DialogBuilder dialogBuilder) {
switch (dialogBuilder.getDialogId()) {
case DIALOG_EXPORT_CHAT_ID:
ExportChatDialogBuilder builder = (ExportChatDialogBuilder) dialogBuilder;
exportChat(builder);
}
}
private void exportChat(ExportChatDialogBuilder dialogBuilder) {
//TODO: retain AsyncTask via retained fragment
new ChatExportAsyncTask(dialogBuilder).execute();
}
private class ChatExportAsyncTask extends AsyncTask<Void, Void, File> {
private ExportChatDialogBuilder builder;
public ChatExportAsyncTask(ExportChatDialogBuilder builder) {
this.builder = builder;
}
@Override
protected File doInBackground(Void... params) {
File file = null;
try {
file = MessageManager.getInstance().exportChat(
actionWithAccount, actionWithUser, builder.getName());
} catch (NetworkException e) {
Application.getInstance().onError(e);
}
return file;
}
@Override
public void onPostExecute(File result) {
if (result != null) {
if (builder.isSendChecked()) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
Uri uri = Uri.fromFile(result);
intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent,
getString(R.string.export_chat)));
} else {
Toast.makeText(ChatViewer.this, R.string.export_chat_done,
Toast.LENGTH_LONG).show();
}
}
}
}
@Override
public void onDecline(DialogBuilder dialogBuilder) {
}
@Override
public void onCancel(DialogBuilder dialogBuilder) {
}
private boolean selectChat(String account, String user) {
for (int position = 0; position < chatViewerAdapter.getCount(); position++)
if (((BaseEntity) chatViewerAdapter.getItem(position)).equals(
......
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.dialog;
import java.io.File;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.xabber.android.data.Application;
import com.xabber.android.data.NetworkException;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.androiddev.R;
public class ChatExportDialogFragment extends ConfirmDialogFragment {
private static final String ACCOUNT = "ACCOUNT";
private static final String USER = "USER";
/**
* @param account
* @param user
* @return
*/
public static DialogFragment newInstance(String account, String user) {
return new ChatExportDialogFragment().putAgrument(ACCOUNT, account)
.putAgrument(USER, user);
}
private String account;
private String user;
private EditText nameView;
private CheckBox sendView;
@Override
protected Builder getBuilder() {
account = getArguments().getString(ACCOUNT);
user = getArguments().getString(USER);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.export_chat_title);
View layout = getActivity().getLayoutInflater().inflate(
R.layout.export_chat, null);
nameView = (EditText) layout.findViewById(R.id.name);
sendView = (CheckBox) layout.findViewById(R.id.send);
nameView.setText(getString(R.string.export_chat_mask, AccountManager
.getInstance().getVerboseName(account), RosterManager
.getInstance().getName(account, user)));
builder.setView(layout);
return builder;
}
@Override
protected boolean onPositiveClick() {
String name = nameView.getText().toString();
if ("".equals(name))
return false;
new ChatExportAsyncTask(account, user, name, sendView.isChecked())
.execute();
return true;
}
private class ChatExportAsyncTask extends AsyncTask<Void, Void, File> {
private final String account;
private final String user;
private final String name;
private final boolean send;
public ChatExportAsyncTask(String account, String user, String name,
boolean send) {
this.account = account;
this.user = user;
this.name = name;
this.send = send;
}
@Override
protected File doInBackground(Void... params) {
try {
return MessageManager.getInstance().exportChat(account, user,
name);
} catch (NetworkException e) {
Application.getInstance().onError(e);
return null;
}
}
@Override
public void onPostExecute(File result) {
if (result == null)
return;
// TODO: Use notification bar to notify about success.
if (send) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
Uri uri = Uri.fromFile(result);
intent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent,
getString(R.string.export_chat)));
} else {
Toast.makeText(getActivity(), R.string.export_chat_done,
Toast.LENGTH_LONG).show();
}
}
}
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.dialog;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.xabber.android.data.account.AccountManager;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.androiddev.R;
public class ExportChatDialogBuilder extends ConfirmDialogBuilder {
private final EditText nameView;
private final CheckBox sendView;
public ExportChatDialogBuilder(Activity activity, int dialogId,
ConfirmDialogListener listener, String account, String user) {
super(activity, dialogId, listener);
setTitle(R.string.export_chat_title);
View layout = activity.getLayoutInflater().inflate(
R.layout.export_chat, null);
nameView = (EditText) layout.findViewById(R.id.name);
sendView = (CheckBox) layout.findViewById(R.id.send);
nameView.setText(activity.getString(R.string.export_chat_mask,
AccountManager.getInstance().getVerboseName(account),
RosterManager.getInstance().getName(account, user)));
setView(layout);
}
@Override
public void onAccept(DialogInterface dialog) {
if ("".equals(getName())) {
Toast.makeText(activity,
activity.getString(R.string.group_is_empty),
Toast.LENGTH_LONG).show();
return;
}
super.onAccept(dialog);
}
public String getName() {
return nameView.getText().toString();
}
public boolean isSendChecked() {
return sendView.isChecked();
}
}
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