1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package androidx.work.integration.testapp.sherlockholmes;
17
18import android.content.Context;
19import android.content.res.AssetManager;
20import android.support.annotation.NonNull;
21import android.util.Log;
22
23import androidx.work.Data;
24import androidx.work.OneTimeWorkRequest;
25import androidx.work.Worker;
26
27import java.io.DataOutputStream;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.InputStream;
31import java.util.HashMap;
32import java.util.Map;
33import java.util.Scanner;
34
35/**
36 * A Worker that counts words of length > 3 and stores the results.
37 */
38public class TextMappingWorker extends Worker {
39
40    private static final String INPUT_FILE = "input_file";
41
42    private Map<String, Integer> mWordCount = new HashMap<>();
43
44    /**
45     * Creates a {@link OneTimeWorkRequest.Builder} with the necessary arguments.
46     *
47     * @param inputFile The input file to process
48     * @return A {@link OneTimeWorkRequest.Builder} with these arguments
49     */
50    public static OneTimeWorkRequest.Builder create(String inputFile) {
51        Data input = new Data.Builder()
52                .putString(INPUT_FILE, inputFile)
53                .build();
54        return new OneTimeWorkRequest.Builder(TextMappingWorker.class).setInputData(input);
55    }
56
57    @Override
58    public @NonNull Result doWork() {
59        Data input = getInputData();
60        String inputFileName = input.getString(INPUT_FILE, null);
61        String outputFileName = "out_" + inputFileName;
62
63        AssetManager assetManager = getApplicationContext().getAssets();
64        InputStream inputStream = null;
65        Scanner scanner = null;
66        try {
67            inputStream = assetManager.open(inputFileName);
68            scanner = new Scanner(inputStream);
69            while (scanner.hasNext()) {
70                String word = scanner.next();
71                if (word.length() > 3) {
72                    int count = 1;
73                    if (mWordCount.containsKey(word)) {
74                        count = mWordCount.get(word) + 1;
75                    }
76                    mWordCount.put(word, count);
77                }
78            }
79        } catch (IOException e) {
80            return Result.FAILURE;
81        } finally {
82            if (scanner != null) {
83                scanner.close();
84            }
85            if (inputStream != null) {
86                try {
87                    inputStream.close();
88                } catch (IOException e) {
89                    // Do nothing.
90                }
91            }
92        }
93
94        FileOutputStream fileOutputStream = null;
95        DataOutputStream dataOutputStream = null;
96        try {
97            fileOutputStream = getApplicationContext()
98                    .openFileOutput(outputFileName, Context.MODE_PRIVATE);
99            dataOutputStream = new DataOutputStream(fileOutputStream);
100            for (Map.Entry<String, Integer> entry : mWordCount.entrySet()) {
101                dataOutputStream.writeUTF(entry.getKey());
102                dataOutputStream.writeInt(entry.getValue());
103            }
104        } catch (IOException e) {
105            return Result.FAILURE;
106        } finally {
107            if (dataOutputStream != null) {
108                try {
109                    dataOutputStream.close();
110                } catch (IOException e) {
111                    // Do nothing.
112                }
113            }
114            if (fileOutputStream != null) {
115                try {
116                    fileOutputStream.close();
117                } catch (IOException e) {
118                    // Do nothing.
119                }
120            }
121        }
122
123        setOutputData(new Data.Builder().putString(INPUT_FILE, outputFileName).build());
124
125        Log.d("Map", "Mapping finished for " + inputFileName);
126        return Result.SUCCESS;
127    }
128}
129