• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 com.android.framebufferizer.utils;
18 
19 import java.io.*;
20 import java.lang.Runtime;
21 import java.nio.file.*;
22 import java.util.Map;
23 import java.util.HashMap;
24 import org.json.JSONObject;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonParseException;
27 import com.google.gson.JsonSyntaxException;
28 import org.json.JSONException;
29 
30 public class Config {
31     public static final String KEY_MAGNIFIED = "magnified";
32     public static final String KEY_INVERTED = "inverted";
33     public static final String KEY_LAYOUT = "layout";
34     public static final String KEY_LOCALE = "locale";
35     public static final String KEY_DEVICE = "device";
36     public static final String KEY_MESSAGE = "message";
37     private static final String fileName = "config.json";
38     private static final String fileLock = ".lock";
39     private static Config _instance = null;
40     private static final String fileDir = System.getProperty("user.home") + File.separator + ".teeui";
41     private static Map<String, Object> configMap = null;
Config()42     private Config() {
43         configMap = new HashMap<>();
44     }
45 
getInstance()46     public static Config getInstance() {
47         if (_instance == null)
48             _instance = new Config();
49 
50         return _instance;
51     }
52 
setValue(String key, Object value)53     public void setValue(String key, Object value){
54         configMap.put(key, value);
55     }
56 
getConfiguration()57     public Map<String, Object> getConfiguration() {
58         Map<String, Object> config = null;
59         try {
60             String content = new String(Files.readAllBytes(Paths.get(fileDir + File.separator + fileName)));
61             Gson gson = new Gson();
62             config = gson.fromJson(content, Map.class);
63         } catch (NoSuchFileException e) {
64             System.err.println("" + fileDir + File.separator + fileName + " file does not exists because the tool is started for the first time.");
65         } catch (SecurityException e) {
66             e.printStackTrace();
67             System.err.println("" + fileDir + File.separator + fileName + " file read/write permission denied.");
68         } catch (IOException e) {
69             e.printStackTrace();
70             System.err.println("Exception while opening file " + fileDir + File.separator + fileName + ".");
71         } catch (JsonSyntaxException e) {
72             e.printStackTrace();
73             System.err.println("Invalid json format for file " + fileDir + File.separator + fileName + ".");
74         } catch (JsonParseException e) {
75             e.printStackTrace();
76             System.err.println("Exception while parsing file " + fileDir + File.separator + fileName + ".");
77         } catch (Exception e) {
78             e.printStackTrace();
79             System.err.println("Exception while getting configuration.");
80         }
81         return config;
82     }
83 
saveConfiguration()84     private void saveConfiguration() {
85         try {
86             File directory = new File(fileDir);
87             directory.mkdirs();
88             File file = new File(directory, fileName);
89             JSONObject jsonObject = new JSONObject(configMap);
90             PrintWriter pw = new PrintWriter(file);
91             pw.write(jsonObject.toString());
92             pw.flush();
93             pw.close();
94         } catch (FileNotFoundException e) {
95             e.printStackTrace();
96             System.err.println("Exception while saving configuration, " + fileDir + File.separator + fileName + " file not found.");
97         } catch (Exception e) {
98             e.printStackTrace();
99             System.err.println("Exception while saving configuration.");
100         }
101     }
102 
createLockFile()103     public boolean createLockFile() {
104         boolean fileCreated = false;
105         try {
106             File directory = new File(fileDir);
107             directory.mkdirs();
108             File file = new File(directory, fileLock);
109             fileCreated = file.createNewFile();
110             if(fileCreated){
111                 Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
112                     public void run() {
113                         try {
114                             saveConfiguration();
115                             file.delete();
116                         } catch (Exception e) {
117                             e.printStackTrace();
118                             System.err.println("Exception while deleting file " + fileDir + File.separator + fileName + ".");
119                         }
120                     }
121                 }));
122             } else {
123                 System.out.println("The lock file already exists. Another instance may be running. If no other instance is running, consider removing the lock file " + fileDir + File.separator + fileLock + " manually and try again.");
124             }
125         } catch (IOException e) {
126             e.printStackTrace();
127             System.err.println("Exception while creating file " + fileDir + File.separator + fileLock + ", exiting application.");
128         }  catch (Exception e) {
129             e.printStackTrace();
130             System.err.println("Exception in createLockFile, exiting application.");
131         }
132         return fileCreated;
133     }
134 }
135