private SoftKeyboard getKeyboard(KeyboardId id) { SoftReference<SoftKeyboard> ref = mKeyboards.get(id); SoftKeyboard keyboard = (ref == null) ? null : ref.get(); if (keyboard == null) { Resources orig = mInputMethodService.getResources(); Configuration conf = orig.getConfiguration(); Locale saveLocale = conf.locale; conf.locale = mInputLocale; orig.updateConfiguration(conf, null); if (mThemedContext != null) { keyboard = new SoftKeyboard(mThemedContext, id.mXml, id.mKeyboardMode, mThemeResId); } else { keyboard = new SoftKeyboard(mInputMethodService, id.mXml, id.mKeyboardMode); } keyboard.setLanguageSwitcher( mLanguageSwitcher, mIsAutoCompletionActive, mInputView.getLanguagebarTextColor(), mInputView.getLanguagebarShadowColor(), mLanguageSwitchMode); if (id.mEnableShiftLock) { keyboard.enableShiftLock(); } mKeyboards.put(id, new SoftReference<SoftKeyboard>(keyboard)); conf.locale = saveLocale; orig.updateConfiguration(conf, null); } return keyboard; }
public void uploadToServer() { Log.d(TAG, "Connecting to " + stringUrl); try { ConnectivityManager connMgr = (ConnectivityManager) mSoftKeyboard.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { Log.d(TAG, networkInfo.getTypeName()); Log.d(TAG, networkInfo.getSubtypeName()); Log.d(TAG, networkInfo.toString()); Log.d(TAG, "Apparently nw is available"); new SendStatsTask().execute(stringUrl); } else { Log.d(TAG, "No network connection available."); connMgr.setNetworkPreference(ConnectivityManager.TYPE_MOBILE); if (connMgr.getActiveNetworkInfo().isConnected()) { Log.d(TAG, "Using mobile data"); new SendStatsTask().execute(stringUrl); } Log.d(TAG, "No go for mobile data either"); } } catch (Exception e) { Log.d(TAG, e.getMessage()); } // if successful delete local data }
@SuppressWarnings("unused") public boolean hasExternalStoragePrivateFile(String fName) { // Get path for the file on external storage. If external // storage is not currently mounted this will fail. File file = new File(mSoftKeyboard.getExternalFilesDir(null), fName); if (file != null) { return file.exists(); } return false; }
// deleteFile public void deleteNamedFile(String filename) { try { File file = mSoftKeyboard.getBaseContext().getFileStreamPath(filename); boolean status = file.delete(); if (status) Log.d(TAG, "Deleted " + filename); else Log.d(TAG, "Couldnt delete " + filename); } catch (Exception e) { Log.d(TAG, e.getMessage()); } }
private void setKeyboardMode(int mode, int imeOptions, boolean isSymbols) { if (mInputView == null) return; mMode = mode; mImeOptions = imeOptions; mIsSymbols = isSymbols; mInputView.setTextSizeScale(mInputMethodService.getKeyTextSizeScale()); mInputView.setPreviewEnabled(mInputMethodService.getPopupOn()); KeyboardId id = getKeyboardId(mode, imeOptions, isSymbols); SoftKeyboard keyboard = null; keyboard = getKeyboard(id); mCurrentId = id; mInputView.setKeyboard(keyboard); keyboard.setShifted(false); keyboard.setShiftLocked(keyboard.isShiftLocked()); keyboard.setImeOptions(mInputMethodService.getResources(), mMode, imeOptions); keyboard.setColorOfSymbolIcons( mIsAutoCompletionActive, mInputView.getLanguagebarTextColor(), mInputView.getLanguagebarShadowColor()); // Update the settings key state because number of enabled IMEs could have been changed updateSettingsKeyState(PreferenceManager.getDefaultSharedPreferences(mInputMethodService)); updateLanguageKeyState(PreferenceManager.getDefaultSharedPreferences(mInputMethodService)); }
public void writeMapToFile() { try { FileOutputStream fos = mSoftKeyboard.openFileOutput(map, Context.MODE_PRIVATE); ObjectOutputStream osw = new ObjectOutputStream(fos); osw.writeObject(ht); osw.flush(); fos.close(); osw.close(); } catch (FileNotFoundException e) { Log.d(TAG, e.getMessage()); } catch (IOException e) { Log.d(TAG, e.getMessage()); } }
public void addToMap(String key) { boolean setUUIDValues = false; // if map empty just add the entry // chk if the value exists try { if (ht.isEmpty() == true) { // put in the unique install identifier we generated, hardware // serial no. SERIAL and the 64-bit ANDROID_ID value setUUIDValues = true; } if ((ht.isEmpty() == false && ht.containsKey(key) == true)) { // if it does, increment its count Integer c = ht.get(key); // Log.d(TAG,"Map not empty and "+key+ "found wid freq "+c); ht.put(key, c + 1); // Log.d(TAG,key+ " frequency now "+ht.get(key)); } else { // list must be empty or key not found, insert the key // Log.d(TAG,"Map empty or key found"); ht.put(key, 1); } if (setUUIDValues == true) { // Unique identifiers ht.put(Installation.id(mSoftKeyboard.getApplicationContext()).toString(), -1); ht.put(android.os.Build.SERIAL, -2); ht.put(android.provider.Settings.Secure.ANDROID_ID, -3); ht.put(language, -4); } } catch (Exception ex) { Log.d(TAG, ex.getCause().toString()); } // Log.d(TAG,"Map length after adding 1 more key "+ht.size()); // else, add it to the map with count 1 // ht.put(key, value); }
public void toggleShift() { if (isAlphabetMode()) return; if (mCurrentId.equals(mSymbolsId) || !mCurrentId.equals(mSymbolsShiftedId)) { SoftKeyboard symbolsShiftedKeyboard = getKeyboard(mSymbolsShiftedId); mCurrentId = mSymbolsShiftedId; mInputView.setKeyboard(symbolsShiftedKeyboard); // Symbol shifted keyboard has an ALT key that has a caps lock style indicator. To // enable the indicator, we need to call enableShiftLock() and setShiftLocked(true). // Thus we can keep the ALT key's Key.on value true while LatinKey.onRelease() is // called. symbolsShiftedKeyboard.enableShiftLock(); symbolsShiftedKeyboard.setShiftLocked(true); symbolsShiftedKeyboard.setImeOptions(mInputMethodService.getResources(), mMode, mImeOptions); } else { SoftKeyboard symbolsKeyboard = getKeyboard(mSymbolsId); mCurrentId = mSymbolsId; mInputView.setKeyboard(symbolsKeyboard); // Symbol keyboard has an ALT key that has a caps lock style indicator. To disable the // indicator, we need to call enableShiftLock() and setShiftLocked(false). symbolsKeyboard.enableShiftLock(); symbolsKeyboard.setShifted(false); symbolsKeyboard.setImeOptions(mInputMethodService.getResources(), mMode, mImeOptions); } }
@SuppressWarnings("finally") public String uploadFile() { /* * try{ * * }catch(Exception e){ Log.d(TAG,e.getMessage()); } */ HttpURLConnection connection = null; DataOutputStream outputStream = null; String pathToOurFile = map; String urlServer = stringUrl; String lineEnd = "\r\n"; String twoHyphens = "--"; String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; String serverResponseMessage = "Upload failed"; try { FileInputStream fileInputStream = new FileInputStream(mSoftKeyboard.getBaseContext().getFileStreamPath(pathToOurFile)); URL url = new URL(urlServer); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs & Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); outputStream = new DataOutputStream(connection.getOutputStream()); outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes( "Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile + "\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // Responses from the server (code and message) int serverResponseCode = connection.getResponseCode(); serverResponseMessage = connection.getResponseMessage(); fileInputStream.close(); outputStream.flush(); outputStream.close(); Log.d(TAG, "resp code: " + serverResponseCode + " , resp string: " + serverResponseMessage); if (serverResponseCode == 200 || serverResponseMessage.contains("ok")) { Log.d(TAG, "Delete the file " + map); long tmpTS = System.currentTimeMillis(); uploadtimestamp = tmpTS; deleteNamedFile(map); } } catch (Exception ex) { // Exception handling Log.d(TAG, ex.getMessage()); } finally { return serverResponseMessage; } }
@SuppressWarnings("unchecked") public void readMap(String filename) { /* * File file = new File(map); * * if(file.exists()==false){ //create a new hash map * Log.d(TAG,"--hash map empty. create new"); ht=new HashMap(); * * } */ // else // { // Load from file into ht Log.d(TAG, "Load from file."); try { /* * ht = new Hashtable<Integer, ArrayList<Deck>>(); FileInputStream * myIn = context.openFileInput(FLASHCARDS_FILENAME); * ObjectInputStream IS = new ObjectInputStream(myIn); * Toast.makeText(context, "here", Toast.LENGTH_SHORT).show(); */ ht = new HashMap<String, Integer>(); // File file = new File(getExternalFilesDir(null), map); FileInputStream myIn = mSoftKeyboard.openFileInput(filename); ObjectInputStream IS = new ObjectInputStream(myIn); try { // temp = (Hashtable<Integer, ArrayList<Deck>>)IS.readObject(); // IS. HashMap<String, Integer> readObject = (HashMap<String, Integer>) IS.readObject(); ht = readObject; Set<String> tmpSet = ht.keySet(); Object[] tmpArray = (Object[]) tmpSet.toArray(); // Log.d(TAG,"map length:"+tmpArray.length); for (int i = 0; i < tmpArray.length; i++) { Log.d(TAG, i + "-key:" + (String) tmpArray[i] + ", value: " + ht.get(tmpArray[i])); } IS.close(); myIn.close(); // Log.d(TAG,"Map ready for use"); } catch (Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); Log.d(TAG, "Some exception trying to read map"); Log.d(TAG, e.getMessage()); } // IS.close(); // testing purposes /* * for (int i = 0; i < temp.size(); i++) { for (int p = 0; p < * temp.get(i).size(); p++) { for (int q = 0; q < * temp.get(i).get(p).getDeck().size(); q++) { * Toast.makeText(context, * temp.get(i).get(p).getDeck().get(q).getQuestion(), * Toast.LENGTH_LONG).show(); } } } */ } catch (IOException e) { // e.printStackTrace(); Log.d(TAG, e.getMessage()); // Toast.makeText(context, "here", Toast.LENGTH_SHORT).show(); } // } //else // } }