Vivek Sekhar | f7ce2fb | 2015-08-11 14:12:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | package com.android.browser; |
| 32 | |
| 33 | import org.codeaurora.swe.utils.Logger; |
| 34 | |
| 35 | import android.content.Context; |
| 36 | |
| 37 | import java.io.BufferedReader; |
| 38 | import java.io.InputStreamReader; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.io.File; |
| 42 | import java.io.FileNotFoundException; |
| 43 | import java.io.FileInputStream; |
| 44 | |
| 45 | public class CommandLineManager { |
| 46 | |
| 47 | private final static String LOGTAG = "CommandLineManager"; |
| 48 | public static final String COMMAND_LINE_FILE = "/data/local/tmp/swe-command-line"; |
| 49 | |
| 50 | // Read from the InputStream object |
| 51 | private static String parseCommandLine (InputStream is) { |
| 52 | BufferedReader br = null; |
| 53 | StringBuilder sb = new StringBuilder(); |
| 54 | String line; |
| 55 | try { |
| 56 | br = new BufferedReader(new InputStreamReader(is)); |
| 57 | while ((line = br.readLine()) != null) { |
| 58 | line = line.trim(); |
| 59 | |
| 60 | int commentIndex = line.indexOf('#'); |
| 61 | if (commentIndex > -1) { |
| 62 | // trim out the comments |
| 63 | line = line.substring(0,commentIndex); |
| 64 | } |
| 65 | |
| 66 | if (line.isEmpty()) |
| 67 | continue; |
| 68 | |
| 69 | // Consider only the uncommented lines |
| 70 | sb.append(line); |
| 71 | sb.append(" "); |
| 72 | } |
| 73 | |
| 74 | // Strip the browser name from the commandline options (First string) if options exists |
| 75 | if (sb.indexOf("--") >= 0) { |
| 76 | sb.delete(0, sb.indexOf(" ")); |
| 77 | } |
| 78 | } catch (IOException e) { |
| 79 | Logger.e(LOGTAG, "Exception:", e); |
| 80 | } finally { |
| 81 | try { |
| 82 | // close the streams and reader |
| 83 | is.close(); |
| 84 | if (br != null) |
| 85 | br.close(); |
| 86 | } catch (IOException e) { |
| 87 | Logger.e(LOGTAG, "Exception:", e); |
| 88 | } |
| 89 | } |
| 90 | return sb.toString(); |
| 91 | } |
| 92 | |
| 93 | // Set the browser options |
| 94 | private static char[] append(InputStream inStreamDefaultCmdLine, |
| 95 | InputStream inStreamUsrCmdLine) { |
| 96 | // bail out since there no file to command line to deal with |
| 97 | if (inStreamDefaultCmdLine == null && inStreamUsrCmdLine == null){ |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | String userArgs = ""; |
| 102 | String defaultArgs = ""; |
| 103 | if (inStreamDefaultCmdLine != null) { |
| 104 | // Reading the default commandline file |
| 105 | // Refers to the internal filename residing in "raw" directory |
| 106 | defaultArgs = parseCommandLine(inStreamDefaultCmdLine); |
| 107 | } |
| 108 | |
| 109 | // Reading the user commandline file |
| 110 | if (inStreamUsrCmdLine != null) { |
| 111 | userArgs = parseCommandLine(inStreamUsrCmdLine); |
| 112 | } |
| 113 | |
| 114 | // Assumption: The user commandline file can be empty or it exists |
| 115 | // with atleast one commandline option. |
| 116 | // Insert the content of the default commandline file in the beginning of |
| 117 | // the user commandline file content. So that, the user settings will get |
| 118 | // the presidence |
| 119 | userArgs = "Browser "+defaultArgs+" "+userArgs; |
| 120 | |
| 121 | |
| 122 | Logger.v(LOGTAG, "(Command Line Arguments): " + userArgs); |
| 123 | |
| 124 | char[] buffer = userArgs.toString().toCharArray(); |
| 125 | return buffer; |
| 126 | } |
| 127 | |
| 128 | public static char[] getCommandLineSwitches(Context context) { |
| 129 | InputStream sweCmdLineStream = |
| 130 | context.getResources().openRawResource(R.raw.swe_command_line); |
| 131 | InputStream usrCmdLineStream = null; |
| 132 | File file = new File(COMMAND_LINE_FILE); |
| 133 | if(file.exists()){ |
| 134 | try { |
| 135 | usrCmdLineStream = new FileInputStream(COMMAND_LINE_FILE); |
| 136 | } catch (FileNotFoundException e) { |
| 137 | } |
| 138 | } |
| 139 | // Set the browser options here |
| 140 | return append(sweCmdLineStream, usrCmdLineStream); |
| 141 | } |
| 142 | } |