Example usage for android.app Activity getSystemService

List of usage examples for android.app Activity getSystemService

Introduction

In this page you can find the example usage for android.app Activity getSystemService.

Prototype

@Override
    public Object getSystemService(@ServiceName @NonNull String name) 

Source Link

Usage

From source file:com.kncwallet.wallet.ui.TransactionsListFragment.java

@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);

    this.activity = (AbstractWalletActivity) activity;
    this.application = (WalletApplication) activity.getApplication();
    this.wallet = application.getWallet();
    this.prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    this.nfcManager = (NfcManager) activity.getSystemService(Context.NFC_SERVICE);
    this.resolver = activity.getContentResolver();
    this.loaderManager = getLoaderManager();
}

From source file:com.microblink.barcode.customcamera.camera2.Camera2Fragment.java

/**
 * Opens the camera specified by {@link Camera2Fragment#mCameraId}.
 *///from  w w  w.ja v  a 2 s  .  c o m
private void openCamera(int width, int height) {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        getActivity().finish();
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:com.example.android.tflitecamerademo.Camera2BasicFragment.java

/**
 * Sets up member variables related to camera.
 *
 * @param width The width of available size for camera preview
 * @param height The height of available size for camera preview
 *///from  w  w  w .j av  a 2s.c om
private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // // For still image captures, we use the largest available size.
            Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());
            imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG,
                    /*maxImages*/ 2);

            // Find out if we need to swap dimension to get the preview size relative to sensor
            // coordinate.
            int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            // noinspection ConstantConditions
            /* Orientation of the camera sensor */
            int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (sensorOrientation == 90 || sensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (sensorOrientation == 0 || sensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
            }

            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            int rotatedPreviewWidth = width;
            int rotatedPreviewHeight = height;
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;

            if (swappedDimensions) {
                rotatedPreviewWidth = height;
                rotatedPreviewHeight = width;
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }

            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }

            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }

            previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth,
                    rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
            } else {
                textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
            }

            this.cameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Failed to access Camera", e);
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance(getString(R.string.camera_error)).show(getChildFragmentManager(),
                FRAGMENT_DIALOG);
    }
}

From source file:fr.xebia.magritte.classifier.lite.Camera2BasicFragment.java

/**
 * Sets up member variables related to camera.
 *
 * @param width The width of available size for camera preview
 * @param height The height of available size for camera preview
 *//*from  ww w . j a v a 2 s  . c  o  m*/
private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // // For still image captures, we use the largest available size.
            Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());
            imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG,
                    /*maxImages*/ 2);

            // Find out if we need to swap dimension to get the preview size relative to sensor
            // coordinate.
            int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            // noinspection ConstantConditions
            /* Orientation of the camera sensor */
            int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (sensorOrientation == 90 || sensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (sensorOrientation == 0 || sensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
            }

            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            int rotatedPreviewWidth = width;
            int rotatedPreviewHeight = height;
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;

            if (swappedDimensions) {
                rotatedPreviewWidth = height;
                rotatedPreviewHeight = width;
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }

            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }

            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }

            previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth,
                    rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
            } else {
                textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
            }

            this.cameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance(getString(R.string.camera_error)).show(getChildFragmentManager(),
                FRAGMENT_DIALOG);
    }
}

From source file:cc.mintcoin.wallet.ui.RequestCoinsFragment.java

@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);

    this.activity = (AbstractBindServiceActivity) activity;
    this.application = (WalletApplication) activity.getApplication();
    this.config = application.getConfiguration();
    this.wallet = application.getWallet();
    this.loaderManager = getLoaderManager();
    this.nfcManager = (NfcManager) activity.getSystemService(Context.NFC_SERVICE);
    this.clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
    this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

From source file:org.hopestarter.wallet.ui.RequestCoinsFragment.java

@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);

    this.activity = (AbstractBindServiceActivity) activity;
    this.application = (WalletApplication) activity.getApplication();
    this.config = application.getConfiguration();
    this.wallet = application.getWallet();
    this.loaderManager = getLoaderManager();
    this.clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
    this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    this.nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
}

From source file:com.example.android.learnmore.Camera2BasicFragment.java

private void openCamera(int width, int height) {
    if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestCameraPermission();/*  w w  w .j  a  v a2s. c  om*/
        return;
    }
    setUpCameraOutputs(width, height);
    configureTransform(width, height);
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
            throw new RuntimeException("Time out waiting to lock camera opening.");
        }
        manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
    }
}

From source file:com.kncwallet.wallet.ui.ReceiveFragment.java

@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);

    this.activity = (AbstractWalletActivity) activity;
    this.application = (WalletApplication) activity.getApplication();
    this.wallet = application.getWallet();
    this.prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    this.loaderManager = getLoaderManager();
    this.nfcManager = (NfcManager) activity.getSystemService(Context.NFC_SERVICE);
    this.clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
}

From source file:org.sufficientlysecure.keychain.ui.dialog.FileDialogFragment.java

/**
 * Creates dialog//w ww.  j a v  a2  s .c om
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Activity activity = getActivity();

    mMessenger = getArguments().getParcelable(ARG_MESSENGER);

    String title = getArguments().getString(ARG_TITLE);
    String message = getArguments().getString(ARG_MESSAGE);
    String defaultFile = getArguments().getString(ARG_DEFAULT_FILE);
    String checkboxText = getArguments().getString(ARG_CHECKBOX_TEXT);
    final int requestCode = getArguments().getInt(ARG_REQUEST_CODE);

    final EditText mFilename;
    final ImageButton mBrowse;
    final CheckBox mCheckBox;

    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setTitle(title);
    alert.setMessage(message);

    View view = inflater.inflate(R.layout.file_dialog, null);

    mFilename = (EditText) view.findViewById(R.id.input);
    mFilename.setText(defaultFile);
    mBrowse = (ImageButton) view.findViewById(R.id.btn_browse);
    mBrowse.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // only .asc or .gpg files
            FileHelper.openFile(activity, mFilename.getText().toString(), "text/plain", requestCode);
        }
    });

    mCheckBox = (CheckBox) view.findViewById(R.id.checkbox);
    if (checkboxText == null) {
        mCheckBox.setEnabled(false);
        mCheckBox.setVisibility(View.GONE);
    } else {
        mCheckBox.setEnabled(true);
        mCheckBox.setVisibility(View.VISIBLE);
        mCheckBox.setText(checkboxText);
    }

    alert.setView(view);

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();

            boolean checked = false;
            if (mCheckBox.isEnabled()) {
                checked = mCheckBox.isChecked();
            }

            // return resulting data back to activity
            Bundle data = new Bundle();
            data.putString(MESSAGE_DATA_FILENAME, mFilename.getText().toString());
            data.putBoolean(MESSAGE_DATA_CHECKED, checked);

            sendMessageToHandler(MESSAGE_OKAY, data);
        }
    });

    alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();
        }
    });
    return alert.create();
}

From source file:id.nci.stm_9.FileDialogFragment.java

/**
 * Creates dialog//from  www . j  a va  2s .c o m
 */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final Activity activity = getActivity();

    mMessenger = getArguments().getParcelable(ARG_MESSENGER);

    String title = getArguments().getString(ARG_TITLE);
    String message = getArguments().getString(ARG_MESSAGE);
    String defaultFile = getArguments().getString(ARG_DEFAULT_FILE);
    String checkboxText = getArguments().getString(ARG_CHECKBOX_TEXT);
    final int requestCode = getArguments().getInt(ARG_REQUEST_CODE);

    final EditText mFilename;
    final ImageButton mBrowse;
    final CheckBox mCheckBox;

    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    AlertDialog.Builder alert = new AlertDialog.Builder(activity);

    alert.setTitle(title);
    alert.setMessage(message);

    View view = inflater.inflate(R.layout.file_dialog, null);

    mFilename = (EditText) view.findViewById(R.id.input);
    mFilename.setText(defaultFile);
    mBrowse = (ImageButton) view.findViewById(R.id.btn_browse);
    mBrowse.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // only .asc or .gpg files
            // setting it to text/plain prevents Cynaogenmod's file manager from selecting asc or gpg types!
            FileHelper.openFile(activity, mFilename.getText().toString(), "*/*", requestCode);
        }
    });

    mCheckBox = (CheckBox) view.findViewById(R.id.checkbox);
    if (checkboxText == null) {
        mCheckBox.setEnabled(false);
        mCheckBox.setVisibility(View.GONE);
    } else {
        mCheckBox.setEnabled(true);
        mCheckBox.setVisibility(View.VISIBLE);
        mCheckBox.setText(checkboxText);
    }

    alert.setView(view);

    alert.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();

            boolean checked = false;
            if (mCheckBox.isEnabled()) {
                checked = mCheckBox.isChecked();
            }

            // return resulting data back to activity
            Bundle data = new Bundle();
            data.putString(MESSAGE_DATA_FILENAME, mFilename.getText().toString());
            data.putBoolean(MESSAGE_DATA_CHECKED, checked);

            sendMessageToHandler(MESSAGE_OKAY, data);
        }
    });

    alert.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            dismiss();
        }
    });
    return alert.create();
}