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