Back to project page filemanager.
The source code is released under:
MIT License
If you think the Android project filemanager listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.binkery.app.filemanager.fragments; /* ww w .j a va2s . co m*/ import java.io.File; import java.lang.reflect.Field; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.Intent; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.text.Selection; import android.text.Spannable; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; import com.binkery.app.filemanager.R; import com.binkery.app.filemanager.commons.Constants; import com.binkery.app.filemanager.utils.Logs; import com.binkery.app.filemanager.utils.ToastUtils; public class RenameDialogFragment extends BaseDialogFragment implements OnClickListener { private static final String TAG = RenameDialogFragment.class .getSimpleName(); private EditText mEditText = null; private TextView mErrorMessage = null; private File mFile = null; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { View view = View .inflate(getActivity(), R.layout.view_file_rename, null); mEditText = (EditText) view.findViewById(R.id.edittext); mErrorMessage = (TextView) view.findViewById(R.id.error); mFile = new File(mPaths[0]); mEditText.setText(mFile.getName()); // set the cursor on the end of the text . CharSequence text = mEditText.getText(); if (text instanceof Spannable) { Spannable spanText = (Spannable) text; Selection.setSelection(spanText, text.length()); } AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.rename); builder.setView(view); builder.setPositiveButton(R.string.ok, this); builder.setNegativeButton(R.string.cancel, this); return builder.create(); } private void dismissDialog(DialogInterface dialog) { try { Field field = dialog.getClass().getSuperclass() .getDeclaredField("mShowing"); field.setAccessible(true); field.set(dialog, true); } catch (Exception e) { } } @Override public void onResume() { super.onResume(); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { InputMethodManager m = (InputMethodManager) mEditText .getContext().getSystemService( Context.INPUT_METHOD_SERVICE); m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } }, 1000); } /** * Keep the dialog displayed when click the button . * * @param dialog */ private void keepDialog(DialogInterface dialog) { try { Field field = dialog.getClass().getSuperclass() .getDeclaredField("mShowing"); field.setAccessible(true); field.set(dialog, false); } catch (Exception e) { } } private void showErrorMessage(String message) { mErrorMessage.setText(message); mErrorMessage.setVisibility(View.VISIBLE); } @Override public void onClick(DialogInterface dialog, int which) { if (which == DialogInterface.BUTTON_NEGATIVE) { dismissDialog(dialog); return; } String name = mEditText.getEditableText().toString(); if (name == null || name.trim().equals("")) { showErrorMessage("Empty File Name"); keepDialog(dialog); return; } if (name.equals(mFile.getName())) { showErrorMessage("Not changed!"); keepDialog(dialog); return; } if (name.indexOf(File.separator) != -1) { showErrorMessage("??????."); keepDialog(dialog); return; } File file = new File(mFile.getParent() + File.separator + name); Logs.i(TAG, "name = " + name + ", path = " + file.getAbsolutePath()); if (mFile.renameTo(file)) { ToastUtils.show(getActivity(), "rename successful"); Intent intent = new Intent(Constants.INTENT_FILE_RENAME); LocalBroadcastManager.getInstance(getActivity()).sendBroadcast( intent); } else { ToastUtils.show(getActivity(), "rename failed"); } dismissDialog(dialog); } }