1/*
2 * Copyright (C) 2009 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 android.gesture;
18
19import android.util.Log;
20import static android.gesture.GestureConstants.*;
21import android.content.Context;
22
23import java.io.File;
24import java.io.FileOutputStream;
25import java.io.FileNotFoundException;
26import java.io.IOException;
27import java.io.FileInputStream;
28import java.io.InputStream;
29import java.lang.ref.WeakReference;
30
31public final class GestureLibraries {
32    private GestureLibraries() {
33    }
34
35    public static GestureLibrary fromFile(String path) {
36        return fromFile(new File(path));
37    }
38
39    public static GestureLibrary fromFile(File path) {
40        return new FileGestureLibrary(path);
41    }
42
43    public static GestureLibrary fromPrivateFile(Context context, String name) {
44        return fromFile(context.getFileStreamPath(name));
45    }
46
47    public static GestureLibrary fromRawResource(Context context, int resourceId) {
48        return new ResourceGestureLibrary(context, resourceId);
49    }
50
51    private static class FileGestureLibrary extends GestureLibrary {
52        private final File mPath;
53
54        public FileGestureLibrary(File path) {
55            mPath = path;
56        }
57
58        @Override
59        public boolean isReadOnly() {
60            return !mPath.canWrite();
61        }
62
63        public boolean save() {
64            if (!mStore.hasChanged()) return true;
65
66            final File file = mPath;
67
68            final File parentFile = file.getParentFile();
69            if (!parentFile.exists()) {
70                if (!parentFile.mkdirs()) {
71                    return false;
72                }
73            }
74
75            boolean result = false;
76            try {
77                //noinspection ResultOfMethodCallIgnored
78                file.createNewFile();
79                mStore.save(new FileOutputStream(file), true);
80                result = true;
81            } catch (FileNotFoundException e) {
82                Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e);
83            } catch (IOException e) {
84                Log.d(LOG_TAG, "Could not save the gesture library in " + mPath, e);
85            }
86
87            return result;
88        }
89
90        public boolean load() {
91            boolean result = false;
92            final File file = mPath;
93            if (file.exists() && file.canRead()) {
94                try {
95                    mStore.load(new FileInputStream(file), true);
96                    result = true;
97                } catch (FileNotFoundException e) {
98                    Log.d(LOG_TAG, "Could not load the gesture library from " + mPath, e);
99                } catch (IOException e) {
100                    Log.d(LOG_TAG, "Could not load the gesture library from " + mPath, e);
101                }
102            }
103
104            return result;
105        }
106    }
107
108    private static class ResourceGestureLibrary extends GestureLibrary {
109        private final WeakReference<Context> mContext;
110        private final int mResourceId;
111
112        public ResourceGestureLibrary(Context context, int resourceId) {
113            mContext = new WeakReference<Context>(context);
114            mResourceId = resourceId;
115        }
116
117        @Override
118        public boolean isReadOnly() {
119            return true;
120        }
121
122        public boolean save() {
123            return false;
124        }
125
126        public boolean load() {
127            boolean result = false;
128            final Context context = mContext.get();
129            if (context != null) {
130                final InputStream in = context.getResources().openRawResource(mResourceId);
131                try {
132                    mStore.load(in, true);
133                    result = true;
134                } catch (IOException e) {
135                    Log.d(LOG_TAG, "Could not load the gesture library from raw resource " +
136                            context.getResources().getResourceName(mResourceId), e);
137                }
138            }
139
140            return result;
141        }
142    }
143}
144