Initial import
[SaveMySugar/android-savemysugar.git] / src / main / java / it / ao2 / savemysugar / SaveMySugarApplication.java
1 /*
2  * SaveMySugar - Exchange messages using the distance between phone calls
3  *
4  * Copyright (C) 2015  Antonio Ospite <ao2@ao2.it>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * adouble with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package it.ao2.savemysugar;
21
22 import android.app.Application;
23 import android.content.SharedPreferences;
24 import android.preference.PreferenceManager;
25 import android.util.Log;
26
27 import it.ao2.util.morse.MorseDistanceModulator;
28 import it.ao2.util.morse.MorseTranslator;
29
30 public class SaveMySugarApplication extends Application {
31
32     private static final String TAG = "SaveMySugar";
33     private MorseDistanceModulator mModulator;
34     private MorseTranslator mTranslator;
35
36     @Override
37     public void onCreate() {
38         super.onCreate();
39
40         mModulator = new MorseDistanceModulator();
41         updateModulatorParams();
42
43         mTranslator = new MorseTranslator();
44     }
45
46     public void updateModulatorParams() {
47         String defaultCallSetupTimeMin = Double.toString(mModulator.DEFAULT_PERIOD_MIN);
48         String defaultCallSetupTimeMax = Double.toString(mModulator.DEFAULT_PERIOD_MAX);
49         String defaultRingTimeMin = Double.toString(mModulator.DEFAULT_PULSE_MIN);
50         String defaultRingTimeMax = Double.toString(mModulator.DEFAULT_PULSE_MAX);
51
52         SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
53         double newCallSetupTimeMin = Double.parseDouble(sharedPrefs.getString("call_setup_time_min", defaultCallSetupTimeMin));
54         double newCallSetupTimeMax = Double.parseDouble(sharedPrefs.getString("call_setup_time_max", defaultCallSetupTimeMax));
55         double newRingTimeMin = Double.parseDouble(sharedPrefs.getString("ring_time_min", defaultRingTimeMin));
56         double newRingTimeMax = Double.parseDouble(sharedPrefs.getString("ring_time_max", defaultRingTimeMax));
57
58         Log.d(TAG, "Call setup time min: " + newCallSetupTimeMin + "\n");
59         Log.d(TAG, "Call setup time max: " + newCallSetupTimeMax + "\n");
60         Log.d(TAG, "Ring time min: " + newRingTimeMin + "\n");
61         Log.d(TAG, "Ring time max: " + newRingTimeMax + "\n");
62
63         // Analog modems don't like to dial a new call immediately after they
64         // hung up the previous one; an extra delay of about one ring time
65         // makes them happy: use newRingTimeMax as the interSymbolDistance.
66         double interSymbolDistance = newRingTimeMax;
67
68         mModulator.setParameters(newCallSetupTimeMin, newCallSetupTimeMax,
69                                  newRingTimeMin, newRingTimeMax,
70                                  interSymbolDistance);
71     }
72
73     public MorseDistanceModulator getModulator() {
74         return mModulator;
75     }
76
77     public MorseTranslator getTranslator() {
78         return mTranslator;
79     }
80 }