Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 82bb3a43 authored by Evan Charlton's avatar Evan Charlton
Browse files

Display application icon and title in alarm config

When setting an application to launch, the icon and title of the app
should be displayed, rather than the intent string/URI.

Change-Id: Ibf6e9e4cac547da55541e5a17b2b2bb584aeec2f
parent 15682bc6
Loading
Loading
Loading
Loading
+65 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

     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.
-->

<!-- Layout for a Preference in a PreferenceActivity. The
     Preference is able to place a specific widget for its particular
     type in the "widget_frame" layout. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize"
    android:paddingLeft="15dp">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

    </RelativeLayout>

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dip"
        android:layout_marginRight="6dip"
        android:layout_gravity="center" />

</LinearLayout>

res/values/attrs.xml

0 → 100644
+21 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

     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.
-->

<resources>
    <declare-styleable name="IntentPreferenceScreen">
        <attr name="icon" format="reference" />
    </declare-styleable>
</resources>
+1 −1
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@
        android:persistent="false"
        android:title="@string/label"
        android:dialogTitle="@string/label" />
    <Preference android:key="intent"
    <com.android.deskclock.IntentPreferenceScreen android:key="intent"
        android:persistent="false"
        android:title="@string/application" />
    <CheckBoxPreference android:key="no_dialog"
+133 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * 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.android.deskclock;

import java.net.URISyntaxException;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

/**
 * Preference used to conveniently store a reference to an Intent. Will
 * automatically update the UI with the application's title and icon as
 * appropriate.
 */
public class IntentPreferenceScreen extends Preference {

    private Drawable mIcon;

    private ImageView mIconView;

    private Intent mIntent;

    public IntentPreferenceScreen(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IntentPreferenceScreen(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayoutResource(R.layout.preference_intent);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.IntentPreferenceScreen, defStyle, 0);
        mIcon = a.getDrawable(R.styleable.IntentPreferenceScreen_icon);
    }

    /**
     * Update the stored intent
     * 
     * @param intent new Intent
     */
    public void updateIntent(Intent intent) {
        mIntent = intent;

        if (mIntent != null) {
            setSummary(intent.toUri(Intent.URI_INTENT_SCHEME));
            try {
                String packageName = mIntent.getComponent().getPackageName();
                PackageManager packageManager = getContext().getPackageManager();
                ApplicationInfo info = packageManager.getApplicationInfo(packageName, 0);
                setSummary(packageManager.getApplicationLabel(info));
                mIcon = packageManager.getApplicationIcon(packageName);
            } catch (NameNotFoundException e) {
                Log.e("Could not find package", e);
            }
        } else {
            setSummary("");
            mIcon = null;
        }

        updateIcon();
    }

    /**
     * Update the stored intent based on the string representation of the
     * intent. See {@link #getIntentString()}.
     * 
     * @param intentUri string representation of the new Intent to use
     */
    public void updateIntent(String intentUri) {
        if (intentUri == null) {
            clearIntent();
        }

        try {
            updateIntent(Intent.parseUri(intentUri, Intent.URI_INTENT_SCHEME));
        } catch (URISyntaxException e) {
            Log.i("Unable to parse intent: " + intentUri);
        }
    }

    /**
     * Erases the stored intent and resets the UI
     */
    public void clearIntent() {
        updateIntent((Intent) null);
    }

    /**
     * @return a string representation of the saved intent or null if no intent
     *         set
     */
    public String getIntentString() {
        if (mIntent == null)
            return null;

        return mIntent.toUri(Intent.URI_INTENT_SCHEME);
    }

    private void updateIcon() {
        if (mIconView != null) {
            mIconView.setImageDrawable(mIcon);
        }
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        mIconView = (ImageView) view.findViewById(android.R.id.icon);
        updateIcon();
    }
}
+11 −12
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.deskclock;

import java.net.URISyntaxException;
import java.util.ArrayList;

import android.app.AlertDialog;
@@ -24,6 +25,9 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.Intent.ShortcutIconResource;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.preference.CheckBoxPreference;
@@ -32,15 +36,9 @@ import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TimePicker;
import android.widget.Toast;

@@ -60,7 +58,7 @@ public class SetAlarm extends PreferenceActivity
    private CheckBoxPreference mVibratePref;
    private RepeatPreference mRepeatPref;
    private MenuItem mTestAlarmItem;
    private Preference mIntentPref;
    private IntentPreferenceScreen mIntentPref;
    private CheckBoxPreference mNoDialogPref;

    private int     mId;
@@ -121,7 +119,7 @@ public class SetAlarm extends PreferenceActivity
        mRepeatPref.setOnPreferenceChangeListener(this);
        mNoDialogPref = (CheckBoxPreference) findPreference("no_dialog");
        mNoDialogPref.setOnPreferenceChangeListener(this);
        mIntentPref = findPreference("intent");
        mIntentPref = (IntentPreferenceScreen) findPreference("intent");

        Intent i = getIntent();
        mId = i.getIntExtra(Alarms.ALARM_ID, -1);
@@ -221,7 +219,8 @@ public class SetAlarm extends PreferenceActivity
        mVibratePref.setChecked(alarm.vibrate);
        // Give the alert uri to the preference.
        mAlarmPref.setAlert(alarm.alert);
        mIntentPref.setSummary(alarm.intent);
        mIntentPref.updateIntent(alarm.intent);

        updateTime();
        updateNoDialog(alarm);
    }
@@ -290,9 +289,9 @@ public class SetAlarm extends PreferenceActivity

        String none = getString(R.string.application_none);
        if (none.equals(data.getStringExtra(Intent.EXTRA_SHORTCUT_NAME))) {
            mIntentPref.setSummary("");
            mIntentPref.clearIntent();
        } else {
            mIntentPref.setSummary(data.toUri(Intent.URI_INTENT_SCHEME));
            mIntentPref.updateIntent(data);
        }
    }

@@ -347,7 +346,7 @@ public class SetAlarm extends PreferenceActivity
        alarm.vibrate = mVibratePref.isChecked();
        alarm.label = mLabel.getText();
        alarm.alert = mAlarmPref.getAlert();
        CharSequence intent = mIntentPref.getSummary();
        CharSequence intent = mIntentPref.getIntentString();
        alarm.intent = intent == null ? "" : intent.toString();
        alarm.noDialog = mNoDialogPref.isChecked();
        updateNoDialog(alarm);