Search in sources :

Example 16 with StoragePathProvider

use of org.odk.collect.android.storage.StoragePathProvider in project collect by opendatakit.

the class ItemsetDbAdapter method createTable.

public boolean createTable(String formHash, String pathHash, String[] columns, String path) {
    StringBuilder sb = new StringBuilder();
    // get md5 of the path to itemset.csv, which is unique per form
    // the md5 is easier to use because it doesn't have chars like '/'
    sb.append("create table ").append(DATABASE_TABLE).append(pathHash).append(" (_id integer primary key autoincrement ");
    for (String column : columns) {
        if (!column.isEmpty()) {
            // add double quotes in case the column is of label:lang
            sb.append(" , \"").append(column).append("\" text ");
        // create database with first line
        }
    }
    sb.append(");");
    String tableCreate = sb.toString();
    Timber.i("create string: %s", tableCreate);
    db.execSQL(tableCreate);
    ContentValues cv = new ContentValues();
    cv.put(KEY_ITEMSET_HASH, formHash);
    cv.put(KEY_PATH, PathUtils.getRelativeFilePath(new StoragePathProvider().getOdkDirPath(StorageSubdirectory.FORMS), path));
    db.insert(ITEMSET_TABLE, null, cv);
    return true;
}
Also used : ContentValues(android.content.ContentValues) StoragePathProvider(org.odk.collect.android.storage.StoragePathProvider)

Example 17 with StoragePathProvider

use of org.odk.collect.android.storage.StoragePathProvider in project collect by opendatakit.

the class ItemsetDbAdapter method delete.

public void delete(String path) {
    StoragePathProvider storagePathProvider = new StoragePathProvider();
    Cursor c = getItemsets(path);
    if (c != null) {
        if (c.getCount() == 1) {
            c.moveToFirst();
            String table = getMd5FromString(PathUtils.getAbsoluteFilePath(storagePathProvider.getOdkDirPath(StorageSubdirectory.FORMS), c.getString(c.getColumnIndex(KEY_PATH))));
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE + table);
        }
        c.close();
    }
    String where = KEY_PATH + "=?";
    String[] whereArgs = { PathUtils.getRelativeFilePath(new StoragePathProvider().getOdkDirPath(StorageSubdirectory.FORMS), path) };
    db.delete(ITEMSET_TABLE, where, whereArgs);
}
Also used : StoragePathProvider(org.odk.collect.android.storage.StoragePathProvider) Cursor(android.database.Cursor)

Example 18 with StoragePathProvider

use of org.odk.collect.android.storage.StoragePathProvider in project collect by opendatakit.

the class CachingQRCodeGenerator method generateQRCode.

@Override
public String generateQRCode(Collection<String> includedPasswordKeys, AppConfigurationGenerator appConfigurationGenerator) throws NoSuchAlgorithmException, IOException, WriterException {
    String preferencesString = appConfigurationGenerator.getAppConfigurationAsJson(includedPasswordKeys);
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(preferencesString.getBytes());
    byte[] messageDigest = md.digest();
    boolean shouldWriteToDisk = true;
    // check if settings directory exists, if not then create one
    File writeDir = new File(new StoragePathProvider().getOdkDirPath(StorageSubdirectory.SETTINGS));
    if (!writeDir.exists()) {
        if (!writeDir.mkdirs()) {
            Timber.e("Error creating directory %s", writeDir.getAbsolutePath());
        }
    }
    File mdCacheFile = new File(getMd5CachePath());
    if (mdCacheFile.exists()) {
        byte[] cachedMessageDigest = FileUtils.read(mdCacheFile);
        /*
             * If the messageDigest generated from the preferences is equal to cachedMessageDigest
             * then don't generate QRCode and read the one saved in disk
             */
        if (Arrays.equals(messageDigest, cachedMessageDigest)) {
            Timber.i("Loading QRCode from the disk...");
            shouldWriteToDisk = false;
        }
    }
    // If the file is not found in the disk or md5Hash not matched
    if (shouldWriteToDisk) {
        Timber.i("Generating QRCode...");
        final long time = System.currentTimeMillis();
        Bitmap bmp = new QRCodeUtils().encode(preferencesString);
        Timber.i("QR Code generation took : %d ms", System.currentTimeMillis() - time);
        Timber.i("Saving QR Code to disk... : %s", getQRCodeFilepath());
        ImageFileUtils.saveBitmapToFile(bmp, getQRCodeFilepath());
        FileUtils.write(mdCacheFile, messageDigest);
        Timber.i("Updated %s file contents", SETTINGS_MD5_FILE);
    }
    return getQRCodeFilepath();
}
Also used : StoragePathProvider(org.odk.collect.android.storage.StoragePathProvider) Bitmap(android.graphics.Bitmap) MessageDigest(java.security.MessageDigest) File(java.io.File)

Example 19 with StoragePathProvider

use of org.odk.collect.android.storage.StoragePathProvider in project collect by opendatakit.

the class WidgetFactory method createWidgetFromPrompt.

public QuestionWidget createWidgetFromPrompt(FormEntryPrompt prompt, PermissionsProvider permissionsProvider) {
    String appearance = Appearances.getSanitizedAppearanceHint(prompt);
    QuestionDetails questionDetails = new QuestionDetails(prompt, readOnlyOverride);
    final QuestionWidget questionWidget;
    switch(prompt.getControlType()) {
        case Constants.CONTROL_INPUT:
            switch(prompt.getDataType()) {
                case Constants.DATATYPE_DATE_TIME:
                    questionWidget = new DateTimeWidget(activity, questionDetails, new DateTimeWidgetUtils());
                    break;
                case Constants.DATATYPE_DATE:
                    questionWidget = new DateWidget(activity, questionDetails, new DateTimeWidgetUtils());
                    break;
                case Constants.DATATYPE_TIME:
                    questionWidget = new TimeWidget(activity, questionDetails, new DateTimeWidgetUtils());
                    break;
                case Constants.DATATYPE_DECIMAL:
                    if (appearance.startsWith(Appearances.EX)) {
                        questionWidget = new ExDecimalWidget(activity, questionDetails, waitingForDataRegistry, stringRequester);
                    } else if (appearance.equals(Appearances.BEARING)) {
                        questionWidget = new BearingWidget(activity, questionDetails, waitingForDataRegistry, (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE));
                    } else {
                        questionWidget = new DecimalWidget(activity, questionDetails);
                    }
                    break;
                case Constants.DATATYPE_INTEGER:
                    if (appearance.startsWith(Appearances.EX)) {
                        questionWidget = new ExIntegerWidget(activity, questionDetails, waitingForDataRegistry, stringRequester);
                    } else {
                        questionWidget = new IntegerWidget(activity, questionDetails);
                    }
                    break;
                case Constants.DATATYPE_GEOPOINT:
                    if (hasAppearance(questionDetails.getPrompt(), PLACEMENT_MAP) || hasAppearance(questionDetails.getPrompt(), MAPS)) {
                        questionWidget = new GeoPointMapWidget(activity, questionDetails, waitingForDataRegistry, new ActivityGeoDataRequester(permissionsProvider, activity));
                    } else {
                        questionWidget = new GeoPointWidget(activity, questionDetails, waitingForDataRegistry, new ActivityGeoDataRequester(permissionsProvider, activity));
                    }
                    break;
                case Constants.DATATYPE_GEOSHAPE:
                    questionWidget = new GeoShapeWidget(activity, questionDetails, waitingForDataRegistry, new ActivityGeoDataRequester(permissionsProvider, activity));
                    break;
                case Constants.DATATYPE_GEOTRACE:
                    questionWidget = new GeoTraceWidget(activity, questionDetails, waitingForDataRegistry, MapProvider.getConfigurator(), new ActivityGeoDataRequester(permissionsProvider, activity));
                    break;
                case Constants.DATATYPE_BARCODE:
                    questionWidget = new BarcodeWidget(activity, questionDetails, waitingForDataRegistry, new CameraUtils());
                    break;
                case Constants.DATATYPE_TEXT:
                    String query = prompt.getQuestion().getAdditionalAttribute(null, "query");
                    if (query != null) {
                        questionWidget = getSelectOneWidget(appearance, questionDetails);
                    } else if (appearance.startsWith(Appearances.PRINTER)) {
                        questionWidget = new ExPrinterWidget(activity, questionDetails, waitingForDataRegistry);
                    } else if (appearance.startsWith(Appearances.EX)) {
                        questionWidget = new ExStringWidget(activity, questionDetails, waitingForDataRegistry, stringRequester);
                    } else if (appearance.contains(Appearances.NUMBERS)) {
                        questionWidget = new StringNumberWidget(activity, questionDetails);
                    } else if (appearance.equals(Appearances.URL)) {
                        questionWidget = new UrlWidget(activity, questionDetails, new ExternalWebPageHelper());
                    } else {
                        questionWidget = new StringWidget(activity, questionDetails);
                    }
                    break;
                default:
                    questionWidget = new StringWidget(activity, questionDetails);
                    break;
            }
            break;
        case Constants.CONTROL_FILE_CAPTURE:
            if (appearance.startsWith(Appearances.EX)) {
                questionWidget = new ExArbitraryFileWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, fileRequester);
            } else {
                questionWidget = new ArbitraryFileWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry);
            }
            break;
        case Constants.CONTROL_IMAGE_CHOOSE:
            if (appearance.equals(Appearances.SIGNATURE)) {
                questionWidget = new SignatureWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, new StoragePathProvider().getTmpImageFilePath());
            } else if (appearance.contains(Appearances.ANNOTATE)) {
                questionWidget = new AnnotateWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, new StoragePathProvider().getTmpImageFilePath());
            } else if (appearance.equals(Appearances.DRAW)) {
                questionWidget = new DrawWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, new StoragePathProvider().getTmpImageFilePath());
            } else if (appearance.startsWith(Appearances.EX)) {
                questionWidget = new ExImageWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, fileRequester);
            } else {
                questionWidget = new ImageWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, new StoragePathProvider().getTmpImageFilePath());
            }
            break;
        case Constants.CONTROL_OSM_CAPTURE:
            questionWidget = new OSMWidget(activity, questionDetails, waitingForDataRegistry, IntentLauncherImpl.INSTANCE, Collect.getInstance().getFormController());
            break;
        case Constants.CONTROL_AUDIO_CAPTURE:
            RecordingRequester recordingRequester = recordingRequesterProvider.create(prompt, useExternalRecorder);
            GetContentAudioFileRequester audioFileRequester = new GetContentAudioFileRequester(activity, IntentLauncherImpl.INSTANCE, waitingForDataRegistry, formEntryViewModel);
            if (appearance.startsWith(Appearances.EX)) {
                questionWidget = new ExAudioWidget(activity, questionDetails, questionMediaManager, audioPlayer, waitingForDataRegistry, fileRequester);
            } else {
                questionWidget = new AudioWidget(activity, questionDetails, questionMediaManager, audioPlayer, recordingRequester, audioFileRequester, new AudioRecorderRecordingStatusHandler(audioRecorder, formEntryViewModel, viewLifecycle));
            }
            break;
        case Constants.CONTROL_VIDEO_CAPTURE:
            if (appearance.startsWith(Appearances.EX)) {
                questionWidget = new ExVideoWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry, fileRequester);
            } else {
                questionWidget = new VideoWidget(activity, questionDetails, questionMediaManager, waitingForDataRegistry);
            }
            break;
        case Constants.CONTROL_SELECT_ONE:
            questionWidget = getSelectOneWidget(appearance, questionDetails);
            break;
        case Constants.CONTROL_SELECT_MULTI:
            // considered in each widget by calls to ExternalDataUtil.getSearchXPathExpression.
            if (appearance.contains(Appearances.MINIMAL)) {
                questionWidget = new SelectMultiMinimalWidget(activity, questionDetails, waitingForDataRegistry);
            } else if (appearance.contains(Appearances.LIST_NO_LABEL)) {
                questionWidget = new ListMultiWidget(activity, questionDetails, false);
            } else if (appearance.contains(Appearances.LIST)) {
                questionWidget = new ListMultiWidget(activity, questionDetails, true);
            } else if (appearance.contains(Appearances.LABEL)) {
                questionWidget = new LabelWidget(activity, questionDetails);
            } else if (appearance.contains(Appearances.IMAGE_MAP)) {
                questionWidget = new SelectMultiImageMapWidget(activity, questionDetails);
            } else {
                questionWidget = new SelectMultiWidget(activity, questionDetails);
            }
            break;
        case Constants.CONTROL_RANK:
            questionWidget = new RankingWidget(activity, questionDetails);
            break;
        case Constants.CONTROL_TRIGGER:
            questionWidget = new TriggerWidget(activity, questionDetails);
            break;
        case Constants.CONTROL_RANGE:
            if (appearance.startsWith(Appearances.RATING)) {
                questionWidget = new RatingWidget(activity, questionDetails);
            } else {
                switch(prompt.getDataType()) {
                    case Constants.DATATYPE_INTEGER:
                        if (prompt.getQuestion().getAppearanceAttr() != null && prompt.getQuestion().getAppearanceAttr().contains(PICKER_APPEARANCE)) {
                            questionWidget = new RangePickerIntegerWidget(activity, questionDetails);
                        } else {
                            questionWidget = new RangeIntegerWidget(activity, questionDetails);
                        }
                        break;
                    case Constants.DATATYPE_DECIMAL:
                        if (prompt.getQuestion().getAppearanceAttr() != null && prompt.getQuestion().getAppearanceAttr().contains(PICKER_APPEARANCE)) {
                            questionWidget = new RangePickerDecimalWidget(activity, questionDetails);
                        } else {
                            questionWidget = new RangeDecimalWidget(activity, questionDetails);
                        }
                        break;
                    default:
                        questionWidget = new StringWidget(activity, questionDetails);
                        break;
                }
            }
            break;
        default:
            questionWidget = new StringWidget(activity, questionDetails);
            break;
    }
    return questionWidget;
}
Also used : SelectMultiImageMapWidget(org.odk.collect.android.widgets.items.SelectMultiImageMapWidget) RecordingRequester(org.odk.collect.android.widgets.utilities.RecordingRequester) ExternalWebPageHelper(org.odk.collect.android.utilities.ExternalWebPageHelper) ActivityGeoDataRequester(org.odk.collect.android.widgets.utilities.ActivityGeoDataRequester) ListMultiWidget(org.odk.collect.android.widgets.items.ListMultiWidget) DateTimeWidgetUtils(org.odk.collect.android.widgets.utilities.DateTimeWidgetUtils) SelectMultiWidget(org.odk.collect.android.widgets.items.SelectMultiWidget) QuestionDetails(org.odk.collect.android.formentry.questions.QuestionDetails) RankingWidget(org.odk.collect.android.widgets.items.RankingWidget) AudioRecorderRecordingStatusHandler(org.odk.collect.android.widgets.utilities.AudioRecorderRecordingStatusHandler) GetContentAudioFileRequester(org.odk.collect.android.widgets.utilities.GetContentAudioFileRequester) StoragePathProvider(org.odk.collect.android.storage.StoragePathProvider) CameraUtils(org.odk.collect.android.utilities.CameraUtils) SelectMultiMinimalWidget(org.odk.collect.android.widgets.items.SelectMultiMinimalWidget) LabelWidget(org.odk.collect.android.widgets.items.LabelWidget)

Aggregations

StoragePathProvider (org.odk.collect.android.storage.StoragePathProvider)19 File (java.io.File)10 Test (org.junit.Test)5 FileOutputStream (java.io.FileOutputStream)3 Bitmap (android.graphics.Bitmap)2 Activity (android.app.Activity)1 Instrumentation (android.app.Instrumentation)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 Paint (android.graphics.Paint)1 Path (android.graphics.Path)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 View (android.view.View)1 AccelerateDecelerateInterpolator (android.view.animation.AccelerateDecelerateInterpolator)1 OvershootInterpolator (android.view.animation.OvershootInterpolator)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 CardView (androidx.cardview.widget.CardView)1 FloatingActionButton (com.google.android.material.floatingactionbutton.FloatingActionButton)1