1/*
2 * Copyright (C) 2014 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 */
16
17package com.android.webview.chromium;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.net.Uri;
23import android.webkit.WebChromeClient.FileChooserParams;
24
25import org.chromium.android_webview.AwContentsClient;
26
27public class FileChooserParamsAdapter extends FileChooserParams {
28    private AwContentsClient.FileChooserParams mParams;
29
30    public static Uri[] parseFileChooserResult(int resultCode, Intent intent) {
31        if (resultCode == Activity.RESULT_CANCELED) {
32            return null;
33        }
34        Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
35                : intent.getData();
36
37        Uri[] uris = null;
38        if (result != null) {
39            uris = new Uri[1];
40            uris[0] = result;
41        }
42        return uris;
43    }
44
45    FileChooserParamsAdapter(AwContentsClient.FileChooserParams params, Context context) {
46        mParams = params;
47    }
48
49    @Override
50    public int getMode() {
51        return mParams.mode;
52    }
53
54    @Override
55    public String[] getAcceptTypes() {
56        if (mParams.acceptTypes == null)
57            return new String[0];
58        return mParams.acceptTypes.split(";");
59    }
60
61    @Override
62    public boolean isCaptureEnabled() {
63        return mParams.capture;
64    }
65
66    @Override
67    public CharSequence getTitle() {
68        return mParams.title;
69    }
70
71    @Override
72    public String getFilenameHint() {
73        return mParams.defaultFilename;
74    }
75
76    @Override
77    public Intent createIntent() {
78        // TODO: Move this code to Aw. Once code is moved
79        // and merged to M37 get rid of this.
80        String mimeType = "*/*";
81        if (mParams.acceptTypes != null && !mParams.acceptTypes.trim().isEmpty())
82            mimeType = mParams.acceptTypes.split(";")[0];
83
84        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
85        i.addCategory(Intent.CATEGORY_OPENABLE);
86        i.setType(mimeType);
87        return i;
88    }
89}
90