/* * Copyright 2012 Tallinn University Centre for Educational Technology * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package ee.htk.dippler.app; import org.json.JSONException; import org.json.JSONObject; import android.content.Intent; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.util.Log; import android.widget.Toast; import ee.htk.dippler.qr.IntentIntegrator; import ee.htk.dippler.qr.IntentResult; public class PreferencesActivity extends PreferenceActivity { private EditTextPreference login_pref; private EditTextPreference blog_pref; private EditTextPreference wsdl_pref; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.prefs); login_pref = (EditTextPreference) findPreference("dippler-username"); blog_pref = (EditTextPreference) findPreference("dippler-blog"); wsdl_pref = (EditTextPreference) findPreference("dippler-bos-wsdl"); } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if ( preference.getKey().equalsIgnoreCase("dippler-qr")) { Toast.makeText(this, R.string.qr_scan_code_blog, Toast.LENGTH_SHORT).show(); IntentIntegrator integrator = new IntentIntegrator(this); integrator.setButtonNoByID(R.string.no_btn); integrator.setButtonYesByID(R.string.yes_btn); integrator.setMessageByID(R.string.no_qr_scanner); integrator.setTitleByID(R.string.no_qr_title); //Scan only QR codes integrator.initiateScan(IntentIntegrator.QR_CODE_TYPES); } return super.onPreferenceTreeClick(preferenceScreen, preference); } public void onActivityResult(int requestCode, int resultCode, Intent intent) { IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); if (scanResult != null && resultCode == RESULT_OK) { // handle scan result try { JSONObject json = new JSONObject(scanResult.getContents()); String login = json.getString("login-name"); String blog = json.getString("blog"); String webservice = json.getString("webservice"); Log.i("dippler-app", "JSON DATA " +login +" "+ blog +" "+webservice); login_pref.setText(login); blog_pref.setText(blog); wsdl_pref.setText(webservice); Toast.makeText(this, R.string.qr_scan_success, Toast.LENGTH_LONG).show(); PreferenceScreen screen = (PreferenceScreen) findPreference("pref-screen-1"); screen.onItemClick(null, null, screen.findPreference("dippler-password").getOrder()+1, 0); } catch (JSONException e) { Toast.makeText(this, R.string.qr_scan_failed, Toast.LENGTH_LONG).show(); } } } }