Java tutorial
/* * Copyright 2016 Simon Zigelli * * 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 com.ze.client.projecto.dialog; import android.app.Dialog; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.TextInputLayout; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import com.ze.client.projecto.R; import com.ze.client.projecto.database.Projector; import com.ze.client.projecto.database.ProjectorDbHelper; import com.ze.client.projecto.database.ProjectorType; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; public class ProjectorDialog extends DialogFragment implements AdapterView.OnItemSelectedListener { private static final String PROJECTOR_ID = "projector"; private static final String TEXT_NAME = "name"; private static final String TEXT_PORT = "port"; private static final String TEXT_HOST = "host"; private static final String ITEM_TYPE = "type"; private static final String ITEM_SCHEME = "scheme"; private long mProjectorId = Projector.NO_ID; private List<ProjectorType> mProjectorTypes = new ArrayList<>(); private List<String> mSchemes = new ArrayList<>(); private Projector mProjector; private EditText mProjectorHost; private EditText mPort; private EditText mName; private Spinner mTypeSpinner; private Spinner mSchemeSpinner; private long mTypeId = -1; public static ProjectorDialog newInstance(long projectorId) { ProjectorDialog f = new ProjectorDialog(); Bundle args = new Bundle(); args.putLong(PROJECTOR_ID, projectorId); f.setArguments(args); return f; } public ProjectorDialog() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mProjectorId = getArguments().getLong(PROJECTOR_ID, Projector.NO_ID); if (mProjectorId != Projector.NO_ID) { mProjector = ProjectorDbHelper.getProjector(getContext(), mProjectorId); mTypeId = mProjector.getProjectorType().getId(); //registerContentObserver(mUri, false, mContentObserver); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_dialog_projector, container); mSchemes.add("http"); mSchemes.add("https"); mProjectorHost = ((TextInputLayout) view.findViewById(R.id.edit_host)).getEditText(); mPort = ((TextInputLayout) view.findViewById(R.id.edit_port)).getEditText(); mName = ((TextInputLayout) view.findViewById(R.id.edit_name)).getEditText(); mTypeSpinner = (Spinner) view.findViewById(R.id.type_spinner); mSchemeSpinner = (Spinner) view.findViewById(R.id.scheme_spinner); TextView dialogTitle = (TextView) view.findViewById(R.id.dialog_title); mProjectorTypes = ProjectorDbHelper.getProjectorTypeList(getContext()); ArrayAdapter<ProjectorType> adapter = new ArrayAdapter<>(this.getContext(), R.layout.dropdown_type, mProjectorTypes); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mTypeSpinner.setAdapter(adapter); ArrayAdapter<String> schemeAdapter = new ArrayAdapter<>(this.getContext(), R.layout.dropdown_type, mSchemes); schemeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSchemeSpinner.setAdapter(schemeAdapter); if (savedInstanceState != null) { mProjectorHost.setText(savedInstanceState.getCharSequence(TEXT_HOST)); mPort.setText(savedInstanceState.getCharSequence(TEXT_PORT)); mName.setText(savedInstanceState.getCharSequence(TEXT_NAME)); mTypeSpinner.setSelection(savedInstanceState.getInt(ITEM_TYPE)); mSchemeSpinner.setSelection(savedInstanceState.getInt(ITEM_SCHEME)); } mTypeSpinner.setOnItemSelectedListener(this); view.findViewById(R.id.button_ok).setOnClickListener(v -> { URI projectorUri = null; try { projectorUri = new URI(mSchemeSpinner.getSelectedItem().toString() + "://" + mProjectorHost.getText().toString() + ":" + mPort.getText().toString()); } catch (URISyntaxException e) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_uri_not_well_formed).setTitle(R.string.dialog_title_error) .create().show(); return; } Projector projector = new Projector(mProjectorId, mName.getText().toString(), projectorUri, ProjectorDbHelper.getProjectorType(getContext(), mTypeId)); if (projector.getId() == Projector.NO_ID) ProjectorDbHelper.insertProjector(getContext(), projector); else ProjectorDbHelper.updateProjector(getContext(), projector); dismiss(); }); view.findViewById(R.id.button_cancel).setOnClickListener(v -> dismiss()); if (mProjector != null) { mProjectorHost.setText(mProjector.getUri().getHost()); mPort.setText(String.valueOf(mProjector.getUri().getPort())); mName.setText(mProjector.getName()); mTypeSpinner.setSelection(getProjectorTypePosition(mProjector.getProjectorType().getId())); mSchemeSpinner.setSelection(schemeAdapter.getPosition(mProjector.getUri().getScheme())); } else { mTypeSpinner.setSelection(0); } if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { dialogTitle.setVisibility(View.GONE); } getDialog().setTitle(R.string.mode_projector); return view; } private int getProjectorTypePosition(long projectorType) { final int size = mProjectorTypes.size(); for (int i = 0; i < size; i++) { if (mProjectorTypes.get(i).getId() == projectorType) return i; } return -1; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.setOnCancelListener(dialog1 -> dismiss()); return dialog; } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { mTypeId = id; } @Override public void onNothingSelected(AdapterView<?> parent) { } @Override public void onSaveInstanceState(Bundle outState) { if (mName != null) outState.putCharSequence(TEXT_NAME, mName.getText()); if (mPort != null) outState.putCharSequence(TEXT_PORT, mPort.getText()); if (mProjectorHost != null) outState.putCharSequence(TEXT_HOST, mProjectorHost.getText()); if (mTypeSpinner != null) outState.putInt(ITEM_TYPE, mTypeSpinner.getSelectedItemPosition()); if (mSchemeSpinner != null) outState.putInt(ITEM_SCHEME, mSchemeSpinner.getSelectedItemPosition()); super.onSaveInstanceState(outState); } }