示例#1
0
MediaScanResult MediaScanner::doProcessDirectory(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) {
    // place to copy file or directory name
    char* fileSpot = path + strlen(path);
    struct dirent* entry;

    if (shouldSkipDirectory(path)) {
        ALOGD("Skipping: %s", path);
        return MEDIA_SCAN_RESULT_OK;
    }

    // Treat all files as non-media in directories that contain a  ".nomedia" file
    if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
        strcpy(fileSpot, ".nomedia");
        if (access(path, F_OK) == 0) {
            ALOGV("found .nomedia, setting noMedia flag");
            noMedia = true;
        }

        // restore path
        fileSpot[0] = 0;
    }

    DIR* dir = opendir(path);
    if (!dir) {
        ALOGW("Error opening directory '%s', skipping: %s.", path, strerror(errno));
        return MEDIA_SCAN_RESULT_SKIPPED;
    }

    MediaScanResult result = MEDIA_SCAN_RESULT_OK;
    while ((entry = readdir(dir))) {
        if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot)
                == MEDIA_SCAN_RESULT_ERROR) {
            result = MEDIA_SCAN_RESULT_ERROR;
            break;
        }
    }
    closedir(dir);
    return result;
}
MediaScanResult MediaScanner::doProcessDirectory(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) {
#ifndef ANDROID_DEFAULT_CODE
    LOGV("doProcessDirectory %s extensions: %s\n", path, extensions);
#endif   

    // place to copy file or directory name
    char* fileSpot = path + strlen(path);
    struct dirent* entry;

    if (shouldSkipDirectory(path)) {
      LOGD("Skipping: %s", path);
      return MEDIA_SCAN_RESULT_OK;
    }

    // Treat all files as non-media in directories that contain a  ".nomedia" file
    if (pathRemaining >= 8 /* strlen(".nomedia") */ ) {
        strcpy(fileSpot, ".nomedia");
        if (access(path, F_OK) == 0) {
            LOGV("found .nomedia, setting noMedia flag\n");
            noMedia = true;
        }

        // restore path
        fileSpot[0] = 0;
    }

    DIR* dir = opendir(path);
    if (!dir) {
        LOGW("Error opening directory '%s', skipping: %s.", path, strerror(errno));
        return MEDIA_SCAN_RESULT_SKIPPED;
    }
#ifndef ANDROID_DEFAULT_CODE
    //--start--find the best file to set default ringtone.
    static bool isSetRingtone = false;
    static bool isSetNotification = false;
    static bool isSetAlarm = false;
    bool needFindFile = false;
    if (isSetRingtone == false) {
        if (strcmp(path, "/system/media/audio/ringtones/") == 0) {
            needFindFile = true;
            isSetRingtone = true;
        }
    }
    if (isSetNotification == false) {
        if (strcmp(path, "/system/media/audio/notifications/") == 0) {
            needFindFile = true;
            isSetNotification = true;
        }
    }
    if (isSetAlarm == false) {
            if (strcmp(path, "/system/media/audio/alarms/") == 0) {
                needFindFile = true;
                isSetAlarm = true;
            }
        }
    if (needFindFile) {
        char* findFile = 0;
        while ((entry = readdir(dir))) {
            const char* name = entry->d_name;
            // ignore "." and ".."
            if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0))) {
                continue;
            }
            int type = entry->d_type;
            if (type == DT_UNKNOWN) {
                // If the type is unknown, stat() the file instead.
                // This is sometimes necessary when accessing NFS mounted filesystems, but
                // could be needed in other cases well.
                struct stat statbuf;
                if (stat(path, &statbuf) == 0) {
                    if (S_ISREG(statbuf.st_mode)) {
                        type = DT_REG;
                    } else if (S_ISDIR(statbuf.st_mode)) {
                        type = DT_DIR;
                    }
                } else {
                    LOGD("stat() failed for %s: %s", path, strerror(errno) );
                }
            }
            if (type == DT_REG) {
                int nameLength = strlen(name);
                if (nameLength > pathRemaining) {
                    // path too long!
                    continue;
                }
                int pathLength = strlen(path);
                strcpy(fileSpot, name);
                //if (fileMatchesExtension(path, extensions)) {
                    if (findFile == 0) {
                        findFile = new char[pathLength + pathRemaining];
                        strcpy(findFile, path);
                    } else {
                        if (strcmp(path, findFile) < 0) {
                            strcpy(findFile, path);
                        }
                    }
               // }
            }
        }
        if (findFile != 0) {
            struct stat statbuf;
            stat(findFile, &statbuf);
            if (statbuf.st_size > 0) {
                LOGD("find the default audio file = %s", findFile);
               // client.scanFile(findFile, statbuf.st_mtime, statbuf.st_size);
               //need audio owner check
               client.scanFile(findFile, statbuf.st_mtime, statbuf.st_size,false,false);
            }
            delete findFile;
            findFile = 0;
           // if (exceptionCheck && exceptionCheck(exceptionEnv)) goto failure;
        } else {
            LOGD("Can not get the best file for ringtone");
        }
        closedir(dir);
        //restore the path
        fileSpot[0] = 0;
        //reopen the dir for android's default deal.
        dir = opendir(path);
        if (!dir) {
            LOGD("opendir %s failed, errno: %d", path, errno);
            return MEDIA_SCAN_RESULT_ERROR;
        }
    }
    //--end--find the best file to set default ringtone.
#endif


    MediaScanResult result = MEDIA_SCAN_RESULT_OK;
    while ((entry = readdir(dir))) {
        if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot)
                == MEDIA_SCAN_RESULT_ERROR) {
            result = MEDIA_SCAN_RESULT_ERROR;
            break;
        }
    }
    closedir(dir);
    return result;
}