src/main/java/de/blau/android/photos/PhotoIndex.java
/**
     * Index any photos found in interesting directories
     * 
     * On Android 11 and later this will only find images that are owned by the app
     */
Index any photos found in interesting directories

On Android 11 and later this will only find images that are owned by the app
Index any photos found in interesting directories On Android 11 and later this will only find images that are owned by the app
private void indexDirectories() {
        Log.d(DEBUG_TAG, "scanning directories");
        File sdcard = Environment.getExternalStorageDirectory(); 
        List<String> mountPoints = new ArrayList<>();
        mountPoints.add(sdcard.getAbsolutePath());
        mountPoints.add(sdcard.getAbsolutePath() + Paths.DIRECTORY_PATH_EXTERNAL_SD_CARD);
        mountPoints.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()); 
        File storageDir = new File(Paths.DIRECTORY_PATH_STORAGE);
        File[] list = storageDir.listFiles();
        if (list != null) {
            for (File f : list) {
                if (f.exists() && f.isDirectory() && !sdcard.getAbsolutePath().equals(f.getAbsolutePath())) {
                    Log.d(DEBUG_TAG, "Adding mount point " + f.getAbsolutePath());
                    mountPoints.add(f.getAbsolutePath());
                }
            }
        }
        SQLiteDatabase db = null;
        Cursor dbresult = null;
        try {
            db = getWritableDatabase();
            dbresult = db.query(SOURCES_TABLE, new String[] { URI_COLUMN, LAST_SCAN_COLUMN, TAG_COLUMN }, TAG_COLUMN + " is NULL", null, null, null, null,
                    null);
            int dirCount = dbresult.getCount();
            dbresult.moveToFirst();
            for (int i = 0; i < dirCount; i++) {
                String dir = dbresult.getString(0);
                long lastScan = dbresult.getLong(1);
                Log.d(DEBUG_TAG, dbresult.getString(0) + " " + dbresult.getLong(1));
                for (String m : mountPoints) {
                    File indir = new File(m, dir);
                    Log.d(DEBUG_TAG, "Scanning directory " + indir.getAbsolutePath());
                    if (indir.exists()) {
                        Cursor dbresult2 = null;
                        try {
                            dbresult2 = db.query(PHOTOS_TABLE, new String[] { "distinct dir" }, "dir LIKE '" + indir.getAbsolutePath() + "%'", null, null, null,
                                    null, null);
                            int dirCount2 = dbresult2.getCount();
                            dbresult2.moveToFirst();
                            for (int j = 0; j < dirCount2; j++) {
                                String dir2 = dbresult2.getString(0);
                                Log.d(DEBUG_TAG, "Checking dir " + dir2);
                                File pDir = new File(dir2);
                                if (!pDir.exists()) {
                                    Log.d(DEBUG_TAG, "Deleting entries for gone dir " + dir2);
                                    db.delete(PHOTOS_TABLE, URI_WHERE, new String[] { dir2 });
                                }
                                dbresult2.moveToNext();
                            }
                            dbresult2.close();
                            scanDir(db, indir.getAbsolutePath(), lastScan);
                            updateSources(db, indir.getName(), null, System.currentTimeMillis());
                        } finally {
                            close(dbresult2);
                        }
                    } else {
                        Log.d(DEBUG_TAG, "Directory " + indir.getAbsolutePath() + " doesn't exist");
                        db.delete(PHOTOS_TABLE, URI_WHERE, new String[] { indir.getAbsolutePath() });
                        db.delete(PHOTOS_TABLE, "dir LIKE ?", new String[] { indir.getAbsolutePath() + "/%" });
                    }
                }
                dbresult.moveToNext();
            }
        } catch (SQLiteException ex) {
            ACRAHelper.nocrashReport(ex, ex.getMessage());
        } finally {
            close(dbresult);
            SavingHelper.close(db);
        }
    }
private void indexDirectories ( ) { Log . d ( DEBUG_TAG , "scanning directories" ) ; File sdcard = Environment . getExternalStorageDirectory ( ) ; List < String > mountPoints = new ArrayList < > ( ) ; mountPoints . add ( sdcard . getAbsolutePath ( ) ) ; mountPoints . add ( sdcard . getAbsolutePath ( ) + Paths . DIRECTORY_PATH_EXTERNAL_SD_CARD ) ; mountPoints . add ( Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_DOWNLOADS ) . getAbsolutePath ( ) ) ; File storageDir = new File ( Paths . DIRECTORY_PATH_STORAGE ) ; File [ ] list = storageDir . listFiles ( ) ; if ( list != null ) { for ( File f : list ) { if ( f . exists ( ) && f . isDirectory ( ) && ! sdcard . getAbsolutePath ( ) . equals ( f . getAbsolutePath ( ) ) ) { Log . d ( DEBUG_TAG , "Adding mount point " + f . getAbsolutePath ( ) ) ; mountPoints . add ( f . getAbsolutePath ( ) ) ; } } } SQLiteDatabase db = null ; Cursor dbresult = null ; try { db = getWritableDatabase ( ) ; dbresult = db . query ( SOURCES_TABLE , new String [ ] { URI_COLUMN , LAST_SCAN_COLUMN , TAG_COLUMN } , TAG_COLUMN + " is NULL" , null , null , null , null , null ) ; int dirCount = dbresult . getCount ( ) ; dbresult . moveToFirst ( ) ; for ( int i = 0 ; i < dirCount ; i ++ ) { String dir = dbresult . getString ( 0 ) ; long lastScan = dbresult . getLong ( 1 ) ; Log . d ( DEBUG_TAG , dbresult . getString ( 0 ) + " " + dbresult . getLong ( 1 ) ) ; for ( String m : mountPoints ) { File indir = new File ( m , dir ) ; Log . d ( DEBUG_TAG , "Scanning directory " + indir . getAbsolutePath ( ) ) ; if ( indir . exists ( ) ) { Cursor dbresult2 = null ; try { dbresult2 = db . query ( PHOTOS_TABLE , new String [ ] { "distinct dir" } , "dir LIKE '" + indir . getAbsolutePath ( ) + "%'" , null , null , null , null , null ) ; int dirCount2 = dbresult2 . getCount ( ) ; dbresult2 . moveToFirst ( ) ; for ( int j = 0 ; j < dirCount2 ; j ++ ) { String dir2 = dbresult2 . getString ( 0 ) ; Log . d ( DEBUG_TAG , "Checking dir " + dir2 ) ; File pDir = new File ( dir2 ) ; if ( ! pDir . exists ( ) ) { Log . d ( DEBUG_TAG , "Deleting entries for gone dir " + dir2 ) ; db . delete ( PHOTOS_TABLE , URI_WHERE , new String [ ] { dir2 } ) ; } dbresult2 . moveToNext ( ) ; } dbresult2 . close ( ) ; scanDir ( db , indir . getAbsolutePath ( ) , lastScan ) ; updateSources ( db , indir . getName ( ) , null , System . currentTimeMillis ( ) ) ; } finally { close ( dbresult2 ) ; } } else { Log . d ( DEBUG_TAG , "Directory " + indir . getAbsolutePath ( ) + " doesn't exist" ) ; db . delete ( PHOTOS_TABLE , URI_WHERE , new String [ ] { indir . getAbsolutePath ( ) } ) ; db . delete ( PHOTOS_TABLE , "dir LIKE ?" , new String [ ] { indir . getAbsolutePath ( ) + "/%" } ) ; } } dbresult . moveToNext ( ) ; } } catch ( SQLiteException ex ) { ACRAHelper . nocrashReport ( ex , ex . getMessage ( ) ) ; } finally { close ( dbresult ) ; SavingHelper . close ( db ) ; } }
Index any photos found in interesting directories
Index any photos found in interesting directories
// determine at least a few of the possible mount points // NOSONAR // NOSONAR // loop over the directories configured // loop over all possible mount points // remove all entries for this directory // Don't crash just report