|
| 1 | +import java.io.OutputStream; |
| 2 | +import java.io.PrintStream; |
| 3 | +import java.util.prefs.Preferences; |
| 4 | +import static java.lang.System.setErr; |
| 5 | +import static java.util.prefs.Preferences.systemRoot; |
| 6 | + |
1 | 7 | //------------------------------------------------------------------------ |
2 | 8 | // Global Functions |
3 | 9 | //------------------------------------------------------------------------ |
@@ -41,7 +47,103 @@ private void checkIs64BitJava() { |
41 | 47 | PopupMessage msg = new PopupMessage("32-bit Java Detected", "OpenBCI GUI v5 and BrainFlow are made for 64-bit Java (Windows, Linux, and Mac). Please update your OS, computer, Processing IDE, or revert to GUI v4 or earlier."); |
42 | 48 | } |
43 | 49 | } |
44 | | - |
| 50 | +/** |
| 51 | +* Determines if elevated rights are required to install/uninstall the application. |
| 52 | +* |
| 53 | +* @return <code>true</code> if elevation is needed to have administrator permissions, <code>false</code> otherwise. |
| 54 | +*/ |
| 55 | +public boolean isElevationNeeded() { |
| 56 | + return isElevationNeeded(null); |
| 57 | +} |
| 58 | +/** |
| 59 | +* Determines if elevated rights are required to install/uninstall the application. |
| 60 | +* |
| 61 | +* @param path the installation path, or <tt>null</tt> if the installation path is unknown |
| 62 | +* @return <tt>true</tt> if elevation is needed to have administrator permissions, <tt>false</tt> otherwise. |
| 63 | +*/ |
| 64 | +public boolean isElevationNeeded(String path) { |
| 65 | + boolean result; |
| 66 | + if (isWindows()) { |
| 67 | + if (path != null) { |
| 68 | + // use the parent path, as that needs to be written to in order to delete the tree |
| 69 | + path = new File(path).getParent(); |
| 70 | + } |
| 71 | + if (path == null || path.trim().length() == 0) { |
| 72 | + path = getWindowsProgramFiles(); |
| 73 | + } |
| 74 | + result = !canWrite(path); |
| 75 | + } else { |
| 76 | + if (path != null) { |
| 77 | + result = !canWrite(path); |
| 78 | + } else { |
| 79 | + result = !System.getProperty("user.name").equals("root"); |
| 80 | + } |
| 81 | + } |
| 82 | + return result; |
| 83 | +} |
| 84 | +/** |
| 85 | +* Determine if user has administrative privileges. |
| 86 | +* |
| 87 | +* @return |
| 88 | +*/ |
| 89 | +public boolean isAdminUser() { |
| 90 | + if (isWindows()) { |
| 91 | + try { |
| 92 | + String NTAuthority = "HKU\\S-1-5-19"; |
| 93 | + String command = "reg query \""+ NTAuthority + "\""; |
| 94 | + Process p = Runtime.getRuntime().exec(command); |
| 95 | + p.waitFor(); |
| 96 | + return (p.exitValue() == 0); |
| 97 | + } catch (Exception e) { |
| 98 | + return canWrite(getWindowsProgramFiles()); |
| 99 | + } |
| 100 | + } |
| 101 | + try { |
| 102 | + String command = "id -u"; |
| 103 | + Process p = Runtime.getRuntime().exec(command); |
| 104 | + p.waitFor(); |
| 105 | + InputStream stdIn = p.getInputStream(); |
| 106 | + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stdIn)); |
| 107 | + String value = bufferedReader.readLine(); |
| 108 | + return value.equals("0"); |
| 109 | + } catch (Exception e) { |
| 110 | + return System.getProperty("user.name").equals("root"); |
| 111 | + } |
| 112 | +} |
| 113 | +/** |
| 114 | +* Tries to determine the Windows Program Files directory. |
| 115 | +* |
| 116 | +* @return the Windows Program Files directory |
| 117 | +*/ |
| 118 | +private String getWindowsProgramFiles() { |
| 119 | + String path = System.getenv("ProgramFiles"); |
| 120 | + if (path == null) { |
| 121 | + path = "C:\\Program Files"; |
| 122 | + } |
| 123 | + return path; |
| 124 | +} |
| 125 | +/** |
| 126 | +* Determines if the specified path can be written to. |
| 127 | +* |
| 128 | +* @param path the path to check |
| 129 | +* @return <tt>true</tt> if the path can be written to, otherwise <tt>false</tt> |
| 130 | +*/ |
| 131 | +private boolean canWrite(String path) { |
| 132 | + File file = new File(path); |
| 133 | + boolean canWrite = file.canWrite(); |
| 134 | + if (canWrite) { |
| 135 | + // make sure that the path can actually be written to, for IZPACK-727 |
| 136 | + try { |
| 137 | + File test = File.createTempFile(".izpackwritecheck", null, file); |
| 138 | + if (!test.delete()) { |
| 139 | + test.deleteOnExit(); |
| 140 | + } |
| 141 | + } catch (IOException exception) { |
| 142 | + canWrite = false; |
| 143 | + } |
| 144 | + } |
| 145 | + return canWrite; |
| 146 | +} |
45 | 147 |
|
46 | 148 | //compute the standard deviation |
47 | 149 | float std(float[] data) { |
|
0 commit comments