Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4c27ca91 authored by Nathan Grebowiec's avatar Nathan Grebowiec Committed by Jorge Ruesga
Browse files

Add ability to toggle .nomedia file in storage locations

Patchset 1) Initial commit
Patchset 2) Fixed tabs and whitespace
Patchset 3) Moved everything to the properties dialog
Patchset 4) Clean up
Patchset 5) Added check for .nomedia dir
Patchset 6) Tab cleanup
Patchset 7)
    Rebase
    Regorganize FsoPropertiesDialog
    Refresh navigation listview if a .nomedia file was created or deleted
    Use FileHelper.deleteFolder instead of DeleteDirCommand
    Mark/Unmark checkbox when user response no or an error occurs (let checkbox as his initial status)
    Internationalize error messages
    Fix nomedia layout id
    Use match_layout instead of fill_parent (deprecated)
    Ensure that the fso is a canonical path
    Use File.exists() to detect the present of .nomedia
    Theme nomedia label
    Format code and clean Up
    Remove W.I.P
Patchset 8)
    Align .nomedia layout
    Split toggleNoMediaFile logic
    Ask user if .nomedia file is not empty
    Fix typos
Patchset 9) Fix typo

Change-Id: I048d1da75f24230380aa7503f2d2c9ad95f9b873
parent 9f860753
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -338,6 +338,31 @@
          android:singleLine="false"
          android:textAppearance="@style/secondary_text_appearance" />
      </TableRow>

      <!-- .nomedia Toggle -->
      <TableRow
        android:id="@+id/fso_skip_media_scan_view"
        android:layout_marginLeft="@dimen/extra_large_margin"
        android:layout_marginRight="@dimen/extra_large_margin">

        <TextView
          android:id="@+id/fso_include_in_media_scan_label"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/default_margin"
          android:gravity="left|center_vertical"
          android:text="@string/fso_properties_dialog_include_in_media_scan"
          android:textAppearance="@style/primary_text_appearance" />

        <CheckBox
          android:id="@+id/fso_include_in_media_scan"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_margin="@dimen/default_margin"
          android:paddingRight="@dimen/double_margin"
          android:gravity="left|center_vertical" />
      </TableRow>

    </TableLayout>
  </ScrollView>

+8 −0
Original line number Diff line number Diff line
@@ -311,6 +311,14 @@
    <item quantity="one">1 files</item>
    <item quantity="other"><xliff:g id="files">%1$d</xliff:g> files</item>
  </plurals>
  <!-- Fso Properties Dialog * Skip media scan -->
  <string name="fso_properties_dialog_include_in_media_scan">Skip media scan:</string>
  <string name="fso_failed_to_allow_media_scan">Failed to allow media scanning</string>
  <string name="fso_failed_to_prevent_media_scan">Failed to prevent media scanning</string>
  <string name="fso_delete_nomedia_dir_title">Delete .nomedia directory</string>
  <string name="fso_delete_nomedia_dir_body">This directory contains a .nomedia directory.\n\nDo you want to delete it and all of its contents?</string>
  <string name="fso_delete_nomedia_non_empty_title">Delete .nomedia file</string>
  <string name="fso_delete_nomedia_non_empty_body">This directory contains a non-empty .nomedia file.\n\nDo you want to delete it?</string>

  <!-- History * History activity title -->
  <string name="history">History</string>
+12 −20
Original line number Diff line number Diff line
@@ -606,6 +606,14 @@ public class NavigationActivity extends Activity
                                FileManagerSettings.SETTINGS_INITIAL_DIR.getId(),
                                (String)FileManagerSettings.
                                    SETTINGS_INITIAL_DIR.getDefaultValue());

                    // Check if request navigation to directory (use as default), and
                    // ensure chrooted and absolute path
                    String navigateTo = getIntent().getStringExtra(EXTRA_NAVIGATE_TO);
                    if (navigateTo != null && navigateTo.length() > 0) {
                        initialDir = navigateTo;
                    }

                    if (NavigationActivity.this.mChRooted) {
                        // Initial directory is the first external sdcard (sdcard, emmc, usb, ...)
                        StorageVolume[] volumes =
@@ -622,29 +630,13 @@ public class NavigationActivity extends Activity
                        }
                    }

                    //Ensure initial is an absolute directory
                    try {
                        initialDir = new File(initialDir).getAbsolutePath();
                    } catch (Throwable e) {
                        Log.e(TAG, "Resolve of initital directory fails", e); //$NON-NLS-1$
                        String msg =
                                getString(
                                        R.string.msgs_settings_invalid_initial_directory,
                                        initialDir);
                        DialogHelper.showToast(NavigationActivity.this, msg, Toast.LENGTH_SHORT);
                        initialDir = FileHelper.ROOT_DIRECTORY;
                    }

                    // Change the current directory to the preference initial directory or the
                    // request if exists
                    String navigateTo = getIntent().getStringExtra(EXTRA_NAVIGATE_TO);
                    if (navigateTo != null && navigateTo.length() > 0) {
                        navigationView.changeCurrentDir(navigateTo);
                    } else {
                    //Ensure that initial directory is an absolute directory
                    initialDir = FileHelper.getAbsPath(initialDir);

                    // Change the current directory to the preference initial directory
                    navigationView.changeCurrentDir(initialDir);
                }
            }
            }
        });
    }

+4 −1
Original line number Diff line number Diff line
@@ -186,7 +186,10 @@ public class PickerActivity extends Activity
        Log.d(TAG, "PickerActivity. type: " + String.valueOf(mimeType)); //$NON-NLS-1$
        if (mimeType != null) {
            if (!MimeTypeHelper.isMimeTypeKnown(this, mimeType)) {
                Log.i(TAG, "Mime type " + mimeType + " unknown, falling back to wildcard.");
                Log.i(TAG,
                        String.format(
                                "Mime type %s unknown, falling back to wildcard.", //$NON-NLS-1$
                                mimeType));
                mimeType = MimeTypeHelper.ALL_MIME_TYPES;
            }
            restrictions.put(DisplayRestrictions.MIME_TYPE_RESTRICTION, mimeType);
+210 −5
Original line number Diff line number Diff line
@@ -33,8 +33,10 @@ import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.cyanogenmod.filemanager.FileManagerApplication;
import com.cyanogenmod.filemanager.R;
@@ -65,9 +67,11 @@ import com.cyanogenmod.filemanager.util.FileHelper;
import com.cyanogenmod.filemanager.util.MimeTypeHelper;
import com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory;
import com.cyanogenmod.filemanager.util.ResourcesHelper;
import com.cyanogenmod.filemanager.util.StorageHelper;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;

/**
 * A class that wraps a dialog for showing information about a {@link FileSystemObject}
 */
@@ -88,7 +92,10 @@ public class FsoPropertiesDialog
     * @hide
     */
    final FileSystemObject mFso;
    private boolean mHasChanged;
    /**
     * @hide
     */
    boolean mHasChanged;

    /**
     * @hide
@@ -100,6 +107,10 @@ public class FsoPropertiesDialog
    private View mPermissionsViewTab;
    private View mInfoView;
    private View mPermissionsView;
    /**
     * @hide
     */
    CheckBox mChkNoMedia;
    /**
     * @hide
     */
@@ -124,7 +135,10 @@ public class FsoPropertiesDialog
     */
    TextView mTvContains;

    private boolean mIgnoreCheckEvents;
    /**
     * @hide
     */
    boolean mIgnoreCheckEvents;
    private boolean mHasPrivileged;
    private final boolean mIsAdvancedMode;

@@ -259,6 +273,7 @@ public class FsoPropertiesDialog
                (TextView)contentView.findViewById(R.id.fso_properties_last_modified);
        TextView tvLastChangedTime =
                (TextView)contentView.findViewById(R.id.fso_properties_last_changed);
        this.mChkNoMedia = (CheckBox)contentView.findViewById(R.id.fso_include_in_media_scan);
        this.mSpnOwner = (Spinner)contentView.findViewById(R.id.fso_properties_owner);
        this.mSpnGroup = (Spinner)contentView.findViewById(R.id.fso_properties_group);
        this.mInfoMsgView = (TextView)contentView.findViewById(R.id.fso_info_msg);
@@ -357,6 +372,18 @@ public class FsoPropertiesDialog
        setPermissionCheckBoxesListener(this.mChkGroupPermission);
        setPermissionCheckBoxesListener(this.mChkOthersPermission);

        // Check if we should show "Skip media scan" toggle
        if (!FileHelper.isDirectory(this.mFso) ||
            !StorageHelper.isPathInStorageVolume(this.mFso.getFullPath())) {
            LinearLayout fsoSkipMediaScanView =
                    (LinearLayout)contentView.findViewById(R.id.fso_skip_media_scan_view);
            fsoSkipMediaScanView.setVisibility(View.GONE);
        } else {
            //attach the click events
            this.mChkNoMedia.setChecked(isNoMediaFilePresent());
            this.mChkNoMedia.setOnCheckedChangeListener(this);
        }

        //Change the tab
        onClick(this.mInfoViewTab);
        this.mIgnoreCheckEvents = false;
@@ -543,8 +570,45 @@ public class FsoPropertiesDialog
     */
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (this.mIgnoreCheckEvents) return;
        switch (buttonView.getId()) {
            case R.id.fso_include_in_media_scan:
                onNoMediaCheckedChanged(buttonView, isChecked);
                break;

            default:
                onPermissionsCheckedChanged(buttonView, isChecked);
                break;
        }
    }

    /**
     * Method that manage a check changed event
     *
     * @param buttonView The checkbox
     * @param isChecked If the checkbox is checked
     */
    private void onNoMediaCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (this.mIgnoreCheckEvents) {
            this.mIgnoreCheckEvents = false;
            return;
        }
        // Checked means "skip media scan"
        final File nomedia = FileHelper.getNoMediaFile(this.mFso);
        if (isChecked) {
            preventMediaScan(nomedia);
        } else {
            allowMediaScan(nomedia);
        }
    }

    /**
     * Method that manage a check changed event
     *
     * @param buttonView The checkbox
     * @param isChecked If the checkbox is checked
     */
    private void onPermissionsCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (this.mIgnoreCheckEvents) return;
        try {
            // Cancel the folder usage command
            cancelFolderUsageCommand();
@@ -620,7 +684,6 @@ public class FsoPropertiesDialog
                    updatePermissions();
                }
            });

        }
    }

@@ -1187,6 +1250,8 @@ public class FsoPropertiesDialog
        theme.setTextColor(this.mContext, (TextView)v, "text_color"); //$NON-NLS-1$
        v = this.mContentView.findViewById(R.id.fso_properties_last_changed);
        theme.setTextColor(this.mContext, (TextView)v, "text_color"); //$NON-NLS-1$
        v = this.mContentView.findViewById(R.id.fso_include_in_media_scan_label);
        theme.setTextColor(this.mContext, (TextView)v, "text_color"); //$NON-NLS-1$

        v = this.mContentView.findViewById(R.id.fso_properties_owner_label);
        theme.setTextColor(this.mContext, (TextView)v, "text_color"); //$NON-NLS-1$
@@ -1225,4 +1290,144 @@ public class FsoPropertiesDialog
        theme.setTextColor(this.mContext, (TextView)v, "text_color"); //$NON-NLS-1$
    }

    /**
     * Method that prevents media scan in the directory (creates a new .nomedia file)
     *
     * @param nomedia The .nomedia file
     */
    private void preventMediaScan(final File nomedia) {
        // Create .nomedia file. The file should not exist here
        try {
            if (!nomedia.createNewFile()) {
                // failed to create .nomedia file
                DialogHelper.showToast(
                    this.mContext,
                    this.mContext.getString(
                            R.string.fso_failed_to_prevent_media_scan),
                    Toast.LENGTH_SHORT);
                this.mIgnoreCheckEvents = true;
                this.mChkNoMedia.setChecked(false);
                return;
            }

            // Refresh the listview
            this.mHasChanged = true;

        } catch (IOException ex) {
            // failed to create .nomedia file
            ExceptionUtil.translateException(this.mContext, ex, true, false, null);
            DialogHelper.showToast(
                this.mContext,
                this.mContext.getString(
                        R.string.fso_failed_to_prevent_media_scan),
                Toast.LENGTH_SHORT);
            this.mIgnoreCheckEvents = true;
            this.mChkNoMedia.setChecked(false);
        }
    }

    /**
     * Method that allows media scan in the directory (removes the .nomedia file)
     *
     * @param nomedia The .nomedia file
     */
    private void allowMediaScan(final File nomedia) {
        // Delete .nomedia file. The file should exist here

        // .nomedia is a directory? Then ask the user prior to remove completely the folder
        if (nomedia.isDirectory()) {
            // confirm removing the dir
            AlertDialog alert = DialogHelper.createYesNoDialog(
                this.mContext,
                R.string.fso_delete_nomedia_dir_title,
                R.string.fso_delete_nomedia_dir_body,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == DialogInterface.BUTTON_POSITIVE) {
                            boolean ret = FileHelper.deleteFolder(nomedia);
                            if (!ret) {
                                DialogHelper.showToast(
                                    FsoPropertiesDialog.this.mContext,
                                    FsoPropertiesDialog.this.mContext.getString(
                                            R.string.fso_failed_to_allow_media_scan),
                                    Toast.LENGTH_SHORT);
                                FsoPropertiesDialog.this.mIgnoreCheckEvents = true;
                                FsoPropertiesDialog.this.mChkNoMedia.setChecked(true);
                                return;
                            }

                            // Refresh the listview
                            FsoPropertiesDialog.this.mHasChanged = true;

                        } else {
                            FsoPropertiesDialog.this.mIgnoreCheckEvents = true;
                            FsoPropertiesDialog.this.mChkNoMedia.setChecked(true);
                        }
                    }
                });
            DialogHelper.delegateDialogShow(this.mContext, alert);

        // .nomedia file is not empty?  Then ask the user prior to remove the file
        } else if (nomedia.length() != 0) {
            // confirm removing non empty file
            AlertDialog alert = DialogHelper.createYesNoDialog(
                this.mContext,
                R.string.fso_delete_nomedia_non_empty_title,
                R.string.fso_delete_nomedia_non_empty_body,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == DialogInterface.BUTTON_POSITIVE) {
                            if (!nomedia.delete()) {
                                DialogHelper.showToast(
                                    FsoPropertiesDialog.this.mContext,
                                    FsoPropertiesDialog.this.mContext.getString(
                                            R.string.fso_failed_to_allow_media_scan),
                                    Toast.LENGTH_SHORT);
                                FsoPropertiesDialog.this.mIgnoreCheckEvents = true;
                                FsoPropertiesDialog.this.mChkNoMedia.setChecked(true);
                                return;
                            }

                            // Refresh the listview
                            FsoPropertiesDialog.this.mHasChanged = true;

                        } else {
                            FsoPropertiesDialog.this.mIgnoreCheckEvents = true;
                            FsoPropertiesDialog.this.mChkNoMedia.setChecked(true);
                        }
                    }
                });
            DialogHelper.delegateDialogShow(this.mContext, alert);

        // Normal .nomedia file
        } else {
            if (!nomedia.delete()) {
                //failed to delete .nomedia file
                DialogHelper.showToast(
                    this.mContext,
                    this.mContext.getString(
                            R.string.fso_failed_to_allow_media_scan),
                    Toast.LENGTH_SHORT);
                FsoPropertiesDialog.this.mIgnoreCheckEvents = true;
                FsoPropertiesDialog.this.mChkNoMedia.setChecked(true);
                return;
            }

            // Refresh the listview
            FsoPropertiesDialog.this.mHasChanged = true;
        }
    }

    /**
     * Method that checks if the .nomedia file is present
     *
     * @return boolean If the .nomedia file is present
     */
    private boolean isNoMediaFilePresent() {
        final File nomedia = FileHelper.getNoMediaFile(this.mFso);
        return nomedia.exists();
    }

}
Loading